[R] Levels in returned data.frame after subset
Ulrik Stervbo
ulrik.stervbo at gmail.com
Sat Sep 4 14:52:31 CEST 2010
Dear List,
When I subset a data.frame, the levels are not re-adjusted (see
example). Why is this? Am I missing out on some basic stuff here?
Thanks
Ulrik
> m <- data.frame(gender = c("M", "M","F"), ht = c(172, 186.5, 165), wt = c(91,99, 74))
> dim(m)
[1] 3 3
> levels(m$gender)
[1] "F" "M"
> s <- subset(m, m$gender == "M")
> dim(s)
[1] 2 3
> levels(s$gender)
[1] "F" "M"
> cat <- sapply(s, is.factor); s[cat] <- lapply(s[cat], factor)
> dim(s)
[1] 2 3
> levels(s$gender)
[1] "M"
More information about the R-help
mailing list