[R] filter rows by value

arun smartpink111 at yahoo.com
Sat May 18 00:43:11 CEST 2013



Hi,
dat1<- read.table(text="
Var  Time
1          51
2          151
3          251
4            234
5          331
6            351
",sep="",header=TRUE)
dat1[!is.na(match(gsub(".*(\\d{2})$","\\1",dat1$Time),51)),]
#  Var Time
#1   1   51
#2   2  151
#3   3  251
#6   6  351
#or
dat1[substr(dat1$Time,nchar(dat1$Time)-1,nchar(dat1$Time))=="51",]
#  Var Time
#1   1   51
#2   2  151
#3   3  251
#6   6  351

#or 
library(stringr)

dat1[str_detect(dat1$Time,"51$"),]
#  Var Time
#1   1   51
#2   2  151
#3   3  251
#6   6  351
#or
dat1[grepl("51$",dat1$Time),]
#  Var Time
#1   1   51
#2   2  151
#3   3  251
#6   6  351
#or
 dat1[str_sub(dat1$Time, start=-2)%in%51,]
  Var Time
#1   1   51
#2   2  151
#3   3  251
#6   6  351
A.K.


----- Original Message -----
From: Ye Lin <yelin at lbl.gov>
To: R help <r-help at r-project.org>
Cc: 
Sent: Friday, May 17, 2013 5:01 PM
Subject: [R] filter rows by value

Hey All,

I want to delete rows based on the last 2 digits on the value in one column
but I dont know how to do that.

Suppose my data looks like this:

Var   Time
1           51
2          151
3           251
*4            234*
*5           331*
6            351

I want to delete the rows that the value in column "Time", the last 2 digit
is not 51, in this case the rows highlighted will be removed.

Thanks for your help!

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