Skip to content
Prev 298964 / 398506 Next

Removing rows if certain elements are found in character string

Hi,
I didn't think about the situation where D comes before T.? I changed my code a little to accommodate that.

dat2<-read.table(text="
1? 0000000000D0000000000000000000000000000000000000 0.007368;
2? 0000000000d0000000000000000000000000000000000000 0.002456;
3? 000000000T00000000000000000000000000000000000000 0.007368;
4? 000000000DT0000000000000000000000000000000000000 0.007368;
5? 000000000T00000000000000000000000000000000000000 0.002456;
6? 000000000Td0000000000000000000000000000000000000 0.002456;
7? 00000000T000000000000000000000000000000000000000 0.007368;
8? 00000000T0D0000000000000000000000000000000000000 0.007368;
9? 00000000T000000000000000000000000000000000000000 0.002456;
10 00000000T0d0000000000000000000000000000000000000 0.002456;
",sep="",header=FALSE)
colnames(dat2)<-c("num","Ch", "count")
dat2[grepl("0T|0Td|0TD",dat2$Ch),]
num?????????????????????????????????????????????? Ch???? count
3??? 3 000000000T00000000000000000000000000000000000000 0.007368;
5??? 5 000000000T00000000000000000000000000000000000000 0.002456;
6??? 6 000000000Td0000000000000000000000000000000000000 0.002456;
7??? 7 00000000T000000000000000000000000000000000000000 0.007368;
8??? 8 00000000T0D0000000000000000000000000000000000000 0.007368;
9??? 9 00000000T000000000000000000000000000000000000000 0.002456;
10? 10 00000000T0d0000000000000000000000000000000000000 0.002456;

A.K.





----- Original Message -----
From: Rui Barradas <ruipbarradas at sapo.pt>
To: Claudia Penaloza <claudiapenaloza at gmail.com>
Cc: r-help at r-project.org
Sent: Monday, July 2, 2012 7:24 PM
Subject: Re: [R] Removing rows if certain elements are found in character string

Hello,

Try regular expressions instead.
In this data.frame, I've changed row nr.4 to have a row with 'D' as 
first non-zero character.

dd <- read.table(text="
ch? ?  count
1? 0000000000D0000000000000000000000000000000000000 0.007368
2? 0000000000d0000000000000000000000000000000000000 0.002456
3? 000000000T00000000000000000000000000000000000000 0.007368
4? 000000000DT0000000000000000000000000000000000000 0.007368
5? 000000000T00000000000000000000000000000000000000 0.002456
6? 000000000Td0000000000000000000000000000000000000 0.002456
7? 00000000T000000000000000000000000000000000000000 0.007368
8? 00000000T0D0000000000000000000000000000000000000 0.007368
9? 00000000T000000000000000000000000000000000000000 0.002456
10 00000000T0d0000000000000000000000000000000000000 0.002456
", header=TRUE)
dd

i1 <- grepl("^([0D]|[0d])*$", dd$ch)
i2 <- grepl("^0*[Dd]", dd$ch)

dd[!i1, ]
dd[!i2, ]
dd[!(i1 | i2), ]


Hope this helps,

Rui Barradas

Em 02-07-2012 23:48, Claudia Penaloza escreveu:
______________________________________________
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.