[R] how to find out whether a string is a factor?

David Winsemius dwinsemius at comcast.net
Tue Jul 12 16:20:46 CEST 2011


On Jul 12, 2011, at 10:12 AM, Sam Steingold wrote:

> I have two data frames:
>> str(ysmd)
> 'data.frame':	8325 obs. of  6 variables:
> $ X.stock                      : Factor w/ 8325 levels  
> "A","AA","AA-",..: 2702 6547 4118 7664 7587 6350 3341 5640 5107  
> 7589 ...
> $ market.cap                   : num  -1.00 2.97e+10 3.54e+08 3.46e 
> +08 -1.00 ...
> $ X52.week.low                 : num  40.2 22.5 27.5 12.2 20.7 ...
> $ X52.week.high                : num  43.3 38.2 35.1 19.2 32.7 ...
> $ X3.month.average.daily.volume: num  154 7862250 16330 205784  
> 14697 ...
> $ X50.day.moving.average.price : num  41.8 36.3 30.5 15.2 29.9 ...
>> str(top1000)
> 'data.frame':	1000 obs. of  1 variable:
> $ V1: Factor w/ 1000 levels "AA","AAI","AAP",..: 146 96 341 814 382  
> 977 66 1 737 595 ...
>
> I want to split ysmd into two new data frames: ysmd.top1000 and
> ysmd.rest so that ysmd.top1000$X.stock only contains factors from
> top1000$V1 and ysmd.rest$X.stock contains all the other factors.
>
> I should be able to just write
>
> ysmd.top1000 <- ysmd[ysmd$X.stock "is in" top1000$V1,]
> ysmd.rest <- ysmd[ysmd$X.stock "not in" top1000$V1,]
>
> but how so I check whether a string is a member of a factor?

?"%in%"

>
> -- 
> Sam Steingold (http://sds.podval.org/) on CentOS release 5.6 (Final)  
> X 11.0.60900031
> http://mideasttruth.com http://truepeace.org
> http://camera.org http://thereligionofpeace.com http://pmw.org.il
> Professionalism is being dispassionate about your work.
>
> ______________________________________________
> 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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list