How can I suppress (not print) line numbers?

How can I suppress (not print) line numbers?
The code reads:

dd<-data.frame(a=gl(2,3),b=gl(3,1,6) ) 
model.matrix( ~a + b + a*b, dd )

Tries:

> dd<-data.frame(a=gl(2,3),b=gl(3,1,6) ) 
> model.matrix( ~a + b + a*b, dd )
  (Intercept) a2 b2 b3 a2:b2 a2:b3
1           1  0  0  0     0     0
2           1  0  1  0     0     0
3           1  0  0  1     0     0
4           1  1  0  0     0     0
5           1  1  1  0     1     0
6           1  1  0  1     0     1
attr(,"assign")
[1] 0 1 2 2 3 3
attr(,"contrasts")
attr(,"contrasts")$a
[1] "contr.treatment"
attr(,"contrasts")$b
[1] "contr.treatment"

> cat(model.matrix( ~a + b + a*b, dd ))
1 1 1 1 1 1 0 0 0 1 1 1 0 1 0 0 1 0 0 0 1 0 0 1 0 0 0 0 1 0 0 0 0 0 0 1
> model.matrix( ~ a + b + a*b, dd )
  (Intercept) a2 b2 b3 a2:b2 a2:b3
1           1  0  0  0     0     0
2           1  0  1  0     0     0
3           1  0  0  1     0     0
4           1  1  0  0     0     0
5           1  1  1  0     1     0
6           1  1  0  1     0     1
attr(,"assign")
[1] 0 1 2 2 3 3
attr(,"contrasts")
attr(,"contrasts")$a
[1] "contr.treatment"
attr(,"contrasts")$b
[1] "contr.treatment"

> dd<-data.frame(a=gl(2,3),b=gl(3,1,6) )
> print(model.matrix( ~a + b + a*b, dd , rowNames=False))
  (Intercept) a2 b2 b3 a2:b2 a2:b3
1           1  0  0  0     0     0
2           1  0  1  0     0     0
3           1  0  0  1     0     0
4           1  1  0  0     0     0
5           1  1  1  0     1     0
6           1  1  0  1     0     1
attr(,"assign")
[1] 0 1 2 2 3 3
attr(,"contrasts")
attr(,"contrasts")$a
[1] "contr.treatment"
attr(,"contrasts")$b
[1] "contr.treatment"

> print(model.matrix( ~a + b + a*b, dd , colNames=False))
  (Intercept) a2 b2 b3 a2:b2 a2:b3
1           1  0  0  0     0     0
2           1  0  1  0     0     0
3           1  0  0  1     0     0
4           1  1  0  0     0     0
5           1  1  1  0     1     0
6           1  1  0  1     0     1
attr(,"assign")
[1] 0 1 2 2 3 3
attr(,"contrasts")
attr(,"contrasts")$a
[1] "contr.treatment"
attr(,"contrasts")$b
[1] "contr.treatment"
+4
source share
3 answers

Unfortunately, there is no way to suppress row names when printing matrices, right? One option is to force data.frame and use an argument row.names print.data.frame():

dd <- data.frame(a=gl(2,3),b=gl(3,1,6));
print(as.data.frame(model.matrix( ~a + b + a*b, dd )),row.names=F);
##  (Intercept) a2 b2 b3 a2:b2 a2:b3
##            1  0  0  0     0     0
##            1  0  1  0     0     0
##            1  0  0  1     0     0
##            1  1  0  0     0     0
##            1  1  1  0     1     0
##            1  1  0  1     0     1
+2
source

You can save the result model.matrix, and then change the outlet names to blank characters.

dd<-data.frame(a=gl(2,3),b=gl(3,1,6) ) 
mm <- model.matrix( ~a + b + a*b, dd )
mm

#  (Intercept) a2 b2 b3 a2:b2 a2:b3
#1           1  0  0  0     0     0
#2           1  0  1  0     0     0
#3           1  0  0  1     0     0
#4           1  1  0  0     0     0
#5           1  1  1  0     1     0
#6           1  1  0  1     0     1
#attr(,"assign")
#[1] 0 1 2 2 3 3
#attr(,"contrasts")
#attr(,"contrasts")$a
#[1] "contr.treatment"

#attr(,"contrasts")$b
#[1] "contr.treatment"

rownames(mm) <- rep("", 6)
#rownames(mm) <- rep("", nrow(mm)) #more general
mm

# (Intercept) a2 b2 b3 a2:b2 a2:b3
#           1  0  0  0     0     0
#           1  0  1  0     0     0
#           1  0  0  1     0     0
#           1  1  0  0     0     0
#           1  1  1  0     1     0
#           1  1  0  1     0     1
#attr(,"assign")
#[1] 0 1 2 2 3 3
#attr(,"contrasts")
#attr(,"contrasts")$a
#[1] "contr.treatment"

#attr(,"contrasts")$b
#[1] "contr.treatment"
+1
source

print.matrix, ?print.default, , :

 ( matrix( model.matrix( ~a + b + a*b, dd ) , nrow(dd)) )
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    0    0    0    0    0
[2,]    1    0    1    0    0    0
[3,]    1    0    0    1    0    0
[4,]    1    1    0    0    0    0
[5,]    1    1    1    0    1    0
[6,]    1    1    0    1    0    1

, , , , :

> print.noRowCol <- function(x) {dimnames(x)<- NULL; print(x)}
> print.noRowCol (model.matrix( ~a + b + a*b, dd ) )
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    0    0    0    0    0
[2,]    1    0    1    0    0    0
[3,]    1    0    0    1    0    0
[4,]    1    1    0    0    0    0
[5,]    1    1    1    0    1    0
[6,]    1    1    0    1    0    1
attr(,"assign")
[1] 0 1 2 2 3 3
attr(,"contrasts")
attr(,"contrasts")$a
[1] "contr.treatment"

attr(,"contrasts")$b
[1] "contr.treatment"
0

All Articles