tags:

views:

75

answers:

1

Hi All,

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.

+7  A: 

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
Fojtasek