如何抑制(不打印)行号?
代码如下:
dd<-data.frame(a=gl(2,3),b=gl(3,1,6) )
model.matrix( ~a + b + a*b, dd )
尝试:
> 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"
答案 0 :(得分:3)
不幸的是,在打印矩阵时似乎没有任何方法可以抑制行名称,不是吗?一种选择是强制使用data.frame并使用print.data.frame()
的row.names
参数:
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
答案 1 :(得分:1)
您可以保存model.matrix
的结果,然后将rownames更改为空字符。
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"
答案 2 :(得分:1)
没有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
另一种选择是构建一个按照您的需要执行的功能,如果您只想抑制rownames或colnames,您可以使逻辑更精细:
> 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"