[R] Group level frequencies

From: Kunzler, Andreas <a.kunzler_at_bzaek.de>
Date: Wed, 16 Jul 2008 10:21:41 +0200


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. Received on Wed 16 Jul 2008 - 08:57:12 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 16 Jul 2008 - 09:31:53 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive