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.
If you only want a count, please try table text <- "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" df <- read.table(textConnection(text),header=T) table(df[,2:3]) If you want sort the data, try order. ----- A R learner. -- View this message in context: http://r.789695.n4.nabble.com/Creating-Enumerated-Variables-tp2290262p2290365.html Sent from the R help mailing list archive at Nabble.com.
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))), ] d$Count <- do.call(c, mapply(seq, 1, as.vector(t(with(d, table(Age, School)))))) d> dID 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). HTH, Dennis On Thu, Jul 15, 2010 at 7:45 AM, jdellava <jdellava@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]]
On Thu, Jul 15, 2010 at 10:45 AM, jdellava <jdellava at 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),]