[R] Group level frequencies
Kunzler, Andreas
a.kunzler at bzaek.de
Wed Jul 16 10:21:41 CEST 2008
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.
More information about the R-help
mailing list