[R] help with "by" command

amalka amalka at gmail.com
Wed Aug 24 20:51:38 CEST 2011


Hello,

I am a new user of R, and I'd be grateful if someone could help me with the
following:

I would like to compute the mean of variable "trust" in dataframe "foo", but
separately for each level of variable V2.  That is, I'd like to compute the
mean of trust at each level of V2.

I have done this:

> tmp <- by(foo, foo$V2, function(x) mean(foo$trust, na.rm=T))
>tmp

Doing this does indeed give me a mean for variable trust at each level of V2
- but the problem is that I get the exact same mean for each level of V2. 
The means are not the really the same across levels of V2, but instead of
getting the mean for each level of V2 I get the overall mean for the
variable in the dataframe listed over and over for each level of V2.  

I have been attempting to figure this out for a while now, but I just can't
seem to figure it out.

Thanks for your time!
Ari

--
View this message in context: http://r.789695.n4.nabble.com/help-with-by-command-tp3766285p3766285.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list