[R] Summarizing factor data in table?

Andy Bunn abunn at whrc.org
Tue Apr 26 19:28:02 CEST 2005


I have a very simple query with regard to summarizing the number of factors
present in a certain snippet of a data frame.
Given the following data frame:

	foo <- data.frame(yr = c(rep(1998,4), rep(1999,4), rep(2000,2)), div =
factor(c(rep(NA,4),"A","B","C","D","A","C")),
      	            org = factor(c(1:4,1:4,1,2)))

I want to get two new variables. Object ndiv would give the number of
divisions by year:
     1998 0
     1999 3
     2000 2
Object norgs would give the number of organizations
     1998 4
     1999 4
     2000 2
I figure xtabs should be able to do it, but I'm stuck without a for loop.
Any suggestions? -Andy




More information about the R-help mailing list