[R] Data.frame manipulation

Petr PIKAL petr.pikal at precheza.cz
Thu Jan 28 08:29:18 CET 2010


HI

r-help-bounces at r-project.org napsal dne 28.01.2010 04:35:29:

> > 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))

Do not use cbind. Its output is a matrix and in this case character 
matrix. Resulting data frame will consist from factors as you can check by 


str(data)

data<-data.frame(id=id,es=es,mod1=mod1,mod2=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:

E.g. aggregate

aggregate(data[, -(3:4)], data[,3:4], mean)
  mod1   mod2 id  es
1    4 calpas  2 0.3
2    1   itas  3 0.2
3    1  other  3 0.3
4    4  other  2 0.1
5    1    wai  3 0.1
6    2    wai  1 0.3

doBy or tapply or ddply from plyr library or ....

Regards
Petr

> >
> > 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 at 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.



More information about the R-help mailing list