Re: [R] removing factor level represented by less than x rows

From: Frank E Harrell Jr <f.harrell_at_vanderbilt.edu>
Date: Sat 09 Jul 2005 - 01:16:23 EST

Mikkel Grum wrote:
> In a number of different situations I'm trying to
> remove factor levels that are represented by less than
> a certain number of rows, e.g. if I had the dataset aa
> below and wanted to remove the species that are
> represented in less than 2 rows:
>
> data(iris)
> aa <- iris[1:101,]
>
> In this case, since I can see that the species
> virginica only has one row, I can write:
>
> table(aa$Species)
> setosa versicolor virginica
> 50 50 1
>
> aa[aa$Species != "virginica", ]
>
> but:
>
> aa[aa$Species == names(table(aa$Species)> 2),]
>
> does not work.
>
> This must be a fairly common task with a straight
> forward solution that I can't see. Any ideas?
>
> Best wishes,
> Mikkel

library(Hmisc)
?combine.levels

This doesn't remove levels but combines infrequent ones though.

Frank

-- 
Frank E Harrell Jr   Professor and Chair           School of Medicine
                      Department of Biostatistics   Vanderbilt University

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Sat Jul 09 01:21:42 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:33:24 EST