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.
well, I'm not completely sure I understand what you want to compute but you may try the following: data.ml <- data.frame(i = 1:8, g = 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)) ################ data.ml[!duplicated(data.ml$g), -1] # or t(sapply(split(data.ml[c("var1", "var2")], data.ml$g), colMeans)) I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: med.kuleuven.be/biostat student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Kunzler, Andreas" <a.kunzler at bzaek.de> To: <r-help at r-project.org> Sent: Wednesday, July 16, 2008 10:21 AM Subject: [R] Group level frequencies> 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 > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >Disclaimer: kuleuven.be/cwis/email_disclaimer.htm
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 > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide 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?