Thank you Dennis--this is perfect!!
AC
On Thu, Jan 28, 2010 at 12:24 AM, Dennis Murphy <djmuser@gmail.com> wrote:
> Hi:
> There are several ways to do this, but these are the most commonly used:
> aggregate() and the ddply() function in package plyr.
>
> (1) plyr solution (using x as the name of your input data frame):
>
> library(plyr)
> > ddply(x, .(id, mod1), summarize, es = mean(es))
> id mod1 es
> 1 1 2 0.30
> 2 2 4 0.15
> 3 3 1 0.20
> > ddply(x, .(id, mod1, mod2), summarize, es = mean(es))
> id mod1 mod2 es
> 1 1 2 wai 0.30
> 2 2 4 calpas 0.20
> 3 2 4 other 0.10
> 4 3 1 itas 0.10
> 5 3 1 wai 0.25
>
> (2) aggregate() function in base R:
>
> > with(x, aggregate(list(es = es), by = list(id = id, mod1 = mod1),
mean))
> id mod1 es
> 1 3 1 0.20
> 2 1 2 0.30
> 3 2 4 0.15
> > with(x, aggregate(list(es = es), by = list(id = id, mod1 = mod1, mod2
> mod2),
> + mean))
> id mod1 mod2 es
> 1 2 4 calpas 0.20
> 2 3 1 itas 0.10
> 3 2 4 other 0.10
> 4 3 1 wai 0.25
> 5 1 2 wai 0.30
>
> Note that enclosing the variable names in lists and 'equating' them
> maintains
> the variable name in the output. Here's what happens if you don't:
>
> > with(x, aggregate(es, list(id, mod1), mean))
> Group.1 Group.2 x
> 1 3 1 0.20
> 2 1 2 0.30
> 3 2 4 0.15
>
> ddply() is a little less painless and sorts the output for you
> automatically.
>
> HTH,
> Dennis
>
> On Wed, Jan 27, 2010 at 7:34 PM, AC Del Re <acdelre@gmail.com> wrote:
>
>> Hi All,
>>
>> I'm conducting a meta-analysis and have taken a data.frame with
multiple
>> rows per
>> study (for each effect size) and performed a weighted average of effect
>> size
>> for
>> each study. This results in a reduced # of rows. I am particularly
>> interested in
>> simply reducing the additional variables in the data.frame to the first
>> row
>> of the
>> corresponding id variable. For example:
>>
>> id<-c(1,2,2,3,3,3)
>> es<-c(.3,.1,.3,.1,.2,.3)
>> mod1<-c(2,4,4,1,1,1)
>>
mod2<-c("wai","other","calpas","wai","itas","other")
>> data<-as.data.frame(cbind(id,es,mod1,mod2))
>>
>> data
>>
>> id es mod1 mod2
>> 1 1 0.3 2 wai
>> 2 2 0.1 4 other
>> 3 2 0.2 4 calpas
>> 4 3 0.1 1 itas
>> 5 3 0.2 1 wai
>> 6 3 0.3 1 wai
>>
>> # I would like to reduce the entire data.frame like this:
>>
>> id es mod1 mod2
>>
>> 1 .30 2 wai
>> 2 .15 4 other
>> 3 .20 1 itas
>>
>> # If possible, I would also like the option of this (collapsing on id
and
>> mod2):
>>
>> id es mod1 mod2
>> 1 .30 2 wai
>> 2 0.1 4 other
>> 2 0.2 4 calpas
>> 3 0.1 1 itas
>> 3 0.25 1 wai
>>
>> Any help is much appreciated!
>>
>> AC Del Re
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> 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]]