[R] count levels per factor level

kjetil brinchmann halvorsen kjetil at entelnet.bo
Fri Jan 10 23:05:11 CET 2003


On 10 Jan 2003 at 15:19, Tord Snall wrote:

> 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().

What about wrapping unique in na.omit:

na.omit(unique(x))

Kjetil Halvorsen

> 
> Please give a hint.
> 
> Thanks a lot in advance!
> 
> Sincerely,
> Tord
> 
> -----------------------------------------------------------------------
> 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!
> ------------------------------------------------------------------------
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> http://www.stat.math.ethz.ch/mailman/listinfo/r-help




More information about the R-help mailing list