[R] Remove Rows Based on Factor

David Winsemius dwinsemius at comcast.net
Mon Apr 15 19:25:36 CEST 2013


On Apr 15, 2013, at 9:58 AM, Sparks, John James wrote:

> Dear R Helpers,
> 
> I did a search for deleting rows based on conditions but wasn't able to
> find an example that addressed the error that I am getting.  I am hoping
> that this is a simple syntax phenomenon that somebody else knows off the
> top of their head.  My apologies for not providing a reproducible example
> but I think that the information given will allow someone to give me a
> hint.
> 
> I want to delete the rows of the data frame ZZ where Index is earlier that
> Jan 1 of 2007.  That Index column is a factor.  When I tired a couple of
> different methods, I got the error shown below.  Can anybody tell me what
> I am doing wrong?  I would really appreciate it.
> 
> --John Sparks
> 
>> str(ZZ)
> 'data.frame':   1584 obs. of  7 variables:
> $ Index   : Factor w/ 1583 levels "2006-04-07","2006-04-10",..: 1 2 3 4 5
> 6 7 8 9 10 ...
> $ Open    : num  17.5 17.6 16.8 17.2 17 ...
> $ High    : num  18.2 17.6 17.2 17.2 17.1 ...
> $ Low     : num  17.3 16.8 16.8 16.8 16.6 ...
> $ Close   : num  17.5 16.8 17.1 16.8 16.7 ...
> $ Volume  : num  23834500 2916000 1453700 991400 967400 ...
> $ Adjusted: num  16.8 16.2 16.4 16.2 16 ...
>> test<-ZZ[ZZ$Index>"2007-01-01",]

It should work using string comparisons:

test<-ZZ[as.character(ZZ$Index) > "2007-01-01", ]

-- 
David

> Warning message:
> In Ops.factor(ZZ$Index, "2007-01-01") : > not meaningful for factors
>> 
>> test<-subset(ZZ,Index>2007-01-01)
> Warning message:
> In Ops.factor(Index, 2007 - 1 - 1) : > not meaningful for factors
> 
> ______________________________________________
> 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
Alameda, CA, USA



More information about the R-help mailing list