Java Mailing List Archive

http://www.r-help.com/

Home » R Help for Statistical Computing »

[R] Levels in returned data.frame after subset

Ulrik Stervbo

2010-09-04

Replies: Find Java Web Hosting

Author LoginPost Reply
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"

______________________________________________
R-help@(protected)
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.
©2008 r-help.com - Jax Systems, LLC, U.S.A.