[R] subset dataset using factor levels instead of factor names

Phil Spector spector at stat.berkeley.edu
Wed Jun 23 01:43:22 CEST 2010


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.
>



More information about the R-help mailing list