开发者

Compute Column in R

What is the difference between the two statements below. They are rendering different outcomes, and since I am trying to come to R from SPSS, I am a little confused.

ds$share.all <- ds[132]/ ds[3]
mean(ds$share.all, na.rm=T)

and

ds$share.all2 <- ds$col1/ ds$Ncol2
mean(ds$share.all2, na.rm=T)

they render the same mean, but on the first, the output is printed as

     col1    
     0.02669424 

and开发者_如何学运维 the second only prints the .02xxxxx.

Any help will be much appreciated.


Indicating a column of a data frame with single brackets (your first example) produces a data frame with just that column, but using the $ operator (as in your second example) is just a vector. Printing something will print the names associated with it if it has names (the col1 in your first example). The data frame you get with ds[132] has a name attribute, but the vector you get with ds$col1 does not. The equivalent of ds$col1 would be to use double instead of single brackets: ds[[132]]. For example:

> x<-data.frame(1:10)
> names(x)<-"var"
> class(x$var)
 [1] "integer"
> class(x[1])
[1] "data.frame"
> identical(x[1],x$var)
[1] FALSE
> identical(x[[1]],x$var)
[1] TRUE
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜