R 如何抑制(不打印)行号?

R 如何抑制(不打印)行号?,r,R,如何抑制(不打印)行号? 代码如下: dd<-data.frame(a=gl(2,3),b=gl(3,1,6) ) model.matrix( ~a + b + a*b, dd ) dd-dd模型矩阵(~a+b+a*b,dd) (截距)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

如何抑制(不打印)行号?
代码如下:

dd<-data.frame(a=gl(2,3),b=gl(3,1,6) ) 
model.matrix( ~a + b + a*b, dd )
dd-dd模型矩阵(~a+b+a*b,dd)
(截距)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
属性(,“分配”)
[1] 0 1 2 2 3 3
属性(,“对比度”)
属性(,“对比度”)$a
[1] “对照治疗”
属性(,“对比度”)$b
[1] “对照治疗”
>类别(模型矩阵(~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
>模型矩阵(~a+b+a*b,dd)
(截距)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
属性(,“分配”)
[1] 0 1 2 2 3 3
属性(,“对比度”)
属性(,“对比度”)$a
[1] “对照治疗”
属性(,“对比度”)$b
[1] “对照治疗”
>dd打印(model.matrix(~a+b+a*b,dd,rowNames=False))
(截距)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
属性(,“分配”)
[1] 0 1 2 2 3 3
属性(,“对比度”)
属性(,“对比度”)$a
[1] “对照治疗”
属性(,“对比度”)$b
[1] “对照治疗”
>打印(model.matrix(~a+b+a*b,dd,colNames=False))
(截距)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
属性(,“分配”)
[1] 0 1 2 2 3 3
属性(,“对比度”)
属性(,“对比度”)$a
[1] “对照治疗”
属性(,“对比度”)$b
[1] “对照治疗”

您可以保存
model.matrix
的结果,然后将行名更改为空字符

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"

dd没有
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"
>print.noRowCol不幸的是,在打印矩阵时似乎没有任何方法抑制行名,不是吗?一个选项是强制使用data.frame并使用的
row.names
参数:

dd
> 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"
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