gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 11:08:06 26 4
gpt4 key购买 nike

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

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"

最佳答案

不幸的是,在打印矩阵时似乎没有任何方法可以抑制行名称,不是吗?一种选择是强制到 data.frame 并使用 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

关于r - 如何抑制(不打印)行号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30955488/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com