[R] Exclude rows by criteria

arun smartpink111 at yahoo.com
Thu Apr 17 20:08:40 CEST 2014


Hi,

If the column is "numeric" class, you don't need as.numeric(as.character(...)).  My response was based on your original post

"
I am analyzing two columns (both are factors):"

Using the same example,
indx1 <- with(dat1,year <=2007 |year >2012)
Warning messages:
1: In Ops.factor(year, 2007) : <= not meaningful for factors
2: In Ops.factor(year, 2012) : > not meaningful for factors
> indx1 [1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA 


If you didn't get this messages, probably the column is already "numeric" class.


To avoid confusions (in the future), it is better to post a reproducible example using ?dput()

A.K.




Thanks for your prompt response.  The 'year' variable is already numeric. Do we need to use 'as.numeric(as.character(year)) <=2007' ? Can we not just use 'year<=2007' ? 


On Thursday, April 17, 2014 5:27 AM, arun <smartpink111 at yahoo.com> wrote:
Hi,
May be this helps:
set.seed(49)
dat1 <- data.frame(group=factor(sample(4,20,replace=TRUE)), year=factor(sample(2003:2014,20,replace=TRUE))) 

indx <- with(dat1, as.numeric(as.character(year)) <=2007 | as.numeric(as.character(year)) >2012 )

with(dat1[!indx,],chisq.test(group,year))
A.K.



I am analyzing two columns (both are factors): group and year by chi-square test: chisq.test(group,year) How can I exclude data (rows) where year is <2007 or >2012 ?
Thanks for your help.




More information about the R-help mailing list