[R] Group level frequencies

jim holtman jholtman at gmail.com
Wed Jul 16 11:05:11 CEST 2008


Is this what you were asking about:

> aggregate(data.ml[,c('var1','var2')], list(data.ml$g), mean)
  Group.1 var1 var2
1       1    3    8
2       2    4    2
3       3    4    4


On Wed, Jul 16, 2008 at 4:21 AM, Kunzler, Andreas <a.kunzler at bzaek.de> wrote:
> Dear List,
>
> I have Multi-level Data
>
> i= Indivitual Level
> g= Group Level
> var1= First Variable of interest
> var2= Second Variable of interest
>
> and I want to count the frequency of "var1" and "var2" on the group
> level.
>
> I found a way, but there must be a much simpler way.
>
> data.ml <-
> data.frame(i=c(1:8),g=as.factor(c(1,1,1,2,2,3,3,3)),var1=c(3,3,3,4,4,4,4
> ,4), var2=c(8,8,8,2,2,4,4,4))
>
> therefore the data looks like
>  i g var1 var2
> 1 1 1    3    8
> 2 2 1    3    8
> 3 3 1    3    8
> 4 4 2    4    2
> 5 5 2    4    2
> 6 6 3    4    4
> 7 7 3    4    4
> 8 8 3    4    4
>
> 1. I used tapply to get the (Group)mean of var1 and var2 in separate
> Equations. The result will be two one-dimensional Array with rownames.
> 2. I Transformed the Arrays in two data.frames
> 3. I merged the data.frames
>
> d.var1 <-
> data.frame(id=rownames(tapply(data.ml$var1,data.ml$g,mean)),var1=as.nume
> ric(tapply(data.ml$var1,data.ml$g,mean)))
>
> d.var2 <-
> data.frame(id=rownames(tapply(data.ml$var2,data.ml$g,mean)),var2=as.nume
> ric(tapply(data.ml$var2,data.ml$g,mean)))
>
> data.gl <- d.var1
> data.gl$var2 <- d.var2$var2
>
> By putting the data.frames d.var1 and d.var2 together in a new
> data.frame data.gl I would like to take control of the "id"-factor
>
> There must be an easier way.
>
> Thank you.
>
> ______________________________________________
> 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list