Hayes, Rachel M
2010-Jun-22 23:37 UTC
[R] subset dataset using factor levels instead of factor names
Hi All, I have a factor variable with 52 levels -with long, annoying names. I want to keep only rows with some variables. I can do this using this code: test1 <- subset(nih2009,ic_name %in% c('NATIONAL EYE INSTITUTE','Veterans Affairs')) dim(test1) [1] 2396 38 But this doesn't work: t1 <- subset(nih2009, ic_name %in% c(27,51)) dim(t1) [1] 0 38 I know there's a way to do this. Any help? Thanks, Rachel [[alternative HTML version deleted]]
Phil Spector
2010-Jun-22 23:43 UTC
[R] subset dataset using factor levels instead of factor names
Rachel - Not exactly a reproducible example, but maybe t1 <- subset(nih2009, ic_name %in% levels(nih2009$ic_name)[c(27,51)]) - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spector at stat.berkeley.edu On Tue, 22 Jun 2010, Hayes, Rachel M wrote:> Hi All, > > I have a factor variable with 52 levels -with long, annoying names. I want to keep only rows with some variables. I can do this using this code: > > test1 <- subset(nih2009,ic_name %in% c('NATIONAL EYE INSTITUTE','Veterans Affairs')) > dim(test1) > > [1] 2396 38 > > But this doesn't work: > > t1 <- subset(nih2009, ic_name %in% c(27,51)) > dim(t1) > [1] 0 38 > > > I know there's a way to do this. Any help? Thanks, > > Rachel > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org 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. >