[R] Selecting groups with R
Michael A. Miller
mmiller3 at iupui.edu
Mon Aug 24 16:06:12 CEST 2009
To drop empty factor levels from a subset, I use the following:
a.subset <- subset(dataset, Color!='BLUE')
ifac <- sapply(a.subset,is.factor)
a.subset[ifac] <- lapply(a.subset[ifac],factor)
Mike
> dataset
Color Score
1 RED 10
2 RED 13
3 RED 12
4 WHITE 22
5 WHITE 27
6 WHITE 25
7 BLUE 18
8 BLUE 17
9 BLUE 16
> table(dataset)
Score
Color 10 12 13 16 17 18 22 25 27
BLUE 0 0 0 1 1 1 0 0 0
RED 1 1 1 0 0 0 0 0 0
WHITE 0 0 0 0 0 0 1 1 1
>
> a.subset <- subset(dataset, Color!='BLUE')
> a.subset
Color Score
1 RED 10
2 RED 13
3 RED 12
4 WHITE 22
5 WHITE 27
6 WHITE 25
>
> table(a.subset)
Score
Color 10 12 13 22 25 27
BLUE 0 0 0 0 0 0
RED 1 1 1 0 0 0
WHITE 0 0 0 1 1 1
>
> ifac <- sapply(a.subset,is.factor)
> a.subset[ifac] <- lapply(a.subset[ifac],factor)
>
> table(a.subset)
Score
Color 10 12 13 22 25 27
RED 1 1 1 0 0 0
WHITE 0 0 0 1 1 1
More information about the R-help
mailing list