Skip to content
Prev 68592 / 398506 Next

Summarizing factor data in table?

Do you want to count the number of non-NA divisions and organizations in 
the data for each year (where duplicates are counted as many times as 
they appear)?

 > tapply(!is.na(foo$div), foo$yr, sum)
1998 1999 2000
    0    4    2
 > tapply(!is.na(foo$org), foo$yr, sum)
1998 1999 2000
    4    4    2
 >

Or perhaps the number of unique non-NA divisions and organizations in 
the data for each year?

 > tapply(foo$div, foo$yr, function(x) length(na.omit(unique(x))))
1998 1999 2000
    0    4    2
 > tapply(foo$org, foo$yr, function(x) length(na.omit(unique(x))))
1998 1999 2000
    4    4    2
 >

(I don't understand where the "3" in your desired output comes from 
though, which maybe indicates I completely misunderstand your request.)
Andy Bunn wrote: