Hi: Hadley's solution is certainly preferred here due to its relative simplicity. I just wanted to correct an error from my earlier post.
On Thu, Jul 15, 2010 at 2:08 PM, Dennis Murphy <djmu...@gmail.com> wrote: > Hi: > > I sincerely hope there's an easier way, but one method to get this is as > follows, > with d as the data frame name of your test data: > > d <- d[order(with(d, Age, School, rev(Grade))), ] > Should be d <- d[with(d, order(Age, School, rev(Grade))), ] > d$Count <- do.call(c, mapply(seq, 1, as.vector(t(with(d, table(Age, > School)))))) > d > > > d > ID Age School Grade Count > 1 1 10 1 98 1 > 3 3 10 1 92 2 > 7 7 10 1 80 3 > 8 8 10 1 79 4 > 2 2 10 2 97 1 > 4 4 11 1 90 1 > 5 5 11 1 80 2 > 6 6 11 2 70 1 > 9 9 11 2 70 2 > > > The code to get the count is a little convoluted: > - first, find the frequency table of Age and School, transpose it and > then unlist into a vector > - use mapply to generate a sequence for each group from 1 up to its > group count; mapply() is necessary to use the counts as a vector argument. > This returns a list of sequences. > - do.call() applies a function (here, c) to an input list, yielding the > vector of groupwise indices we wanted. Basically, it flattens the list. This > is what we assign to d$Count. > > Side note: I didn't get the correct ordering the first time, but I did the > second time (2.11.1 64bit, Windows 7). > And now we know why :) D. > > HTH, > Dennis > > > On Thu, Jul 15, 2010 at 7: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? >> >> Thank you. >> >> -- >> View this message in context: >> http://r.789695.n4.nabble.com/Creating-Enumerated-Variables-tp2290262p2290262.html >> Sent from the R help mailing list archive at Nabble.com. >> >> ______________________________________________ >> 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. >> > > [[alternative HTML version deleted]] ______________________________________________ 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.