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
______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.