[R] count levels per factor level

Tord Snall tord.snall at ebc.uu.se
Fri Jan 10 15:26:02 CET 2003

Dear all,

I would be really happy for help with the following because I will treat
many columns the same way.

nlevels per ObjektID is what I want, but nlevels returns all levels
occurring in cpy.busk$TradArt:

buskartant<- aggregate(list(trash = cpy.busk$TradArt), list(ObjektID =
cpy.busk$ObjektID), nlevels) 

But as you can see I use it below anyway. I could use any function instaed
of nlevels beacuse now I just want the ObjektID in the right order.

Greg Warnes kindly helped me with this solution: 
group.list <- split(cpy.busk$TradArt, cpy.busk$ObjektID)
buskartant$buskartant <- sapply( group.list, function(x) length(unique(x)) )

But the probelm is that unique considers NA as a category, and there is no
na.rm argument in unique().

Please give a hint.

Thanks a lot in advance!


Tord Snäll
Avd. f växtekologi, Evolutionsbiologiskt centrum, Uppsala universitet
Dept. of Plant Ecology, Evolutionary Biology Centre, Uppsala University
Villavägen 14			
SE-752 36 Uppsala, Sweden
Tel: 018-471 28 82 (int +46 18 471 28 82) (work)
Tel: 018-25 71 33 (int +46 18 25 71 33) (home)
Fax: 018-55 34 19 (int +46 18 55 34 19) (work)
E-mail: Tord.Snall at ebc.uu.se
Check this: http://www.vaxtbio.uu.se/resfold/snall.htm!

More information about the R-help mailing list