问题。数字变量与非数字变量和“名称”变量功能
> scores=cbind(UNCA.score, A.score, B.score, U.m.A, U.m.B)
> names(scores)=c('UNCA.scores', 'A.scores', 'B.scores','UNCA.minus.A', 'UNCA.minus.B')
> names(scores)
[1] "UNCA.scores" "A.scores" "B.scores" "UNCA.minus.A" "UNCA.minus.B"
> summary(UNCA.scores)
X6.69230769230769
Min. : 4.154
1st Qu.: 7.333
Median : 8.308
Mean : 8.451
3rd Qu.: 9.538
Max. :12.000
> is.numeric(UNCA.scores)
[1] FALSE
> is.numeric(scores[,1])
[1] TRUE
我的问题是,UNCA.scores和scores[,1]有什么区别? UNCA.scores 是 data.frame“分数”中的第一列,但它们不是同一件事,因为一个是数字,另一个不是。
如果 UNCA.scores 只是这里的一个标签,我怎样才能使其等同于 'scores[,1]?
谢谢!
> scores=cbind(UNCA.score, A.score, B.score, U.m.A, U.m.B)
> names(scores)=c('UNCA.scores', 'A.scores', 'B.scores','UNCA.minus.A', 'UNCA.minus.B')
> names(scores)
[1] "UNCA.scores" "A.scores" "B.scores" "UNCA.minus.A" "UNCA.minus.B"
> summary(UNCA.scores)
X6.69230769230769
Min. : 4.154
1st Qu.: 7.333
Median : 8.308
Mean : 8.451
3rd Qu.: 9.538
Max. :12.000
> is.numeric(UNCA.scores)
[1] FALSE
> is.numeric(scores[,1])
[1] TRUE
My question is, what is the difference between UNCA.scores and scores[,1]? UNCA.scores is the first column in the data.frame 'scores', but they are not the same thing, since one is numeric and the other isn't.
If UNCA.scores is just a label here how can I make it be equivalent to 'scores[,1]?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
scores
是一个矩阵,而不是一个 data.frame。使用data.frame
创建data.frame,而不是cbind
。矩阵中的所有列都具有相同的类型,在本例中使用 cbind 将所有元素强制为数字。
scores
is a matrix, not a data.frame. Usedata.frame
to create a data.frame, notcbind
.All columns in a matrix are of the same type, and in this case using
cbind
coerces all elements to numeric.您发布的代码似乎有些有趣。如果代码开始时还没有名为 UCNA.scores 的对象,则尝试将其传递给汇总函数应该会产生错误,因为您不能仅按名称引用矩阵列。另一件事是,为矩阵分配名称是为元素命名,而不是为列命名。如果您对 data.frame 进行评分并在创建后立即附加它,那么您的输出就是我所期望的。
即使抛开这些事情,我也无法重现你的问题。如果确实缺少零件,您能否发布一些示例数据和/或显示其他情况?
It seems like there's something funny with the code you posted. If there isn't already an object called UCNA.scores when your code begins, trying to pass it to the summary function should produce an error, because you can't just refer to a matrix column by name. Another thing is that assigning names to a matrix names the elements, not the columns. Your output is what I would expect if you made scores a data.frame and attached it right after creating it.
Even with these things aside, I can't reproduce your problem. Could you post some example data and/or show anything else going on if there really are parts missing?