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)) >tmpDoing 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.
Hi Ari, Try this instead with(foo, tapply(V2, trust, mean, na.rm = TRUE)) See ?tapply and ?with for more information. HTH, Jorge On Wed, Aug 24, 2011 at 2:51 PM, amalka <> wrote:> 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. > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
---------- Forwarded message ---------- From: Ken Hutchison <vicvoncastle@gmail.com> Date: Wed, Aug 24, 2011 at 6:06 PM Subject: Re: [R] help with "by" command To: amalka <amalka@gmail.com> ?tapply or more specifically ?ave Hope this helps, Ken On Wed, Aug 24, 2011 at 2:51 PM, amalka <amalka@gmail.com> wrote:> 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. > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Thanks very much for the help. I ended up getting it to work with one small change: by(foo, foo$V2, function(foo) mean(foo$trust, na.rm=T)) thanks again, Ari -- View this message in context: http://r.789695.n4.nabble.com/help-with-by-command-tp3766285p3779622.html Sent from the R help mailing list archive at Nabble.com.