开发者

R Question. Numeric variable vs. Non-numeric and "names" function

开发者 https://www.devze.com 2022-12-31 18:43 出处:网络
> 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\')
 > 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 th开发者_开发百科e 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!


scores is a matrix, not a data.frame. Use data.frame to create a data.frame, not cbind.

All columns in a matrix are of the same type, and in this case using cbind coerces all elements to numeric.


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消