On Thu, Jul 15, 2010 at 10:45 AM, jdellava <jdell...@vcu.edu> wrote: > > Hi, > > I am trying to create a variable counting the number of individuals based on > two variables. I am able to do it or one variable, but not two. In SAS I was > able to sort by two variables and use a first. statement to create the > counts based on both. Here is an example: > > What I have > ID Age School Grade > 1 10 1 98 > 2 10 2 97 > 3 10 1 92 > 4 11 1 90 > 5 11 1 80 > 6 11 2 70 > 7 10 1 80 > 8 10 1 79 > 9 11 2 70 > > What I need > ID Age School Grade School Count > 1 10 1 98 1 > 3 10 1 92 2 > 7 10 1 80 3 > 8 10 1 79 4 > 2 10 2 97 1 > 4 11 1 90 1 > 5 11 1 80 2 > 6 11 2 70 1 > 9 11 2 70 2 > > I want to create counts of individuals age 10 in school 1 then age 10 in > school two (the what I need set) > > Anyway to do this? >
The first statement uses ave to create the sequences and the second statement sorts it: xx2 <- transform(xx, Count = ave(ID, Age, School, FUN = seq_along)) xx2[order(xx2$Age, xx$School),] ______________________________________________ R-help@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.