[R] Removing rows if certain elements are found in character string
arun
smartpink111 at yahoo.com
Tue Jul 3 01:29:32 CEST 2012
Hi,
Try this:
dat1<-read.table(text="
1 0000000000D0000000000000000000000000000000000000 0.007368;
2 0000000000d0000000000000000000000000000000000000 0.002456;
3 000000000T00000000000000000000000000000000000000 0.007368;
4 000000000TD0000000000000000000000000000000000000 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(dat1)<-c("num","Ch", "count")
#I guess this is what you wanted.
dat1[grepl("TD|Td|T",dat1$Ch),]
num Ch count
3 3 000000000T00000000000000000000000000000000000000 0.007368;
4 4 000000000TD0000000000000000000000000000000000000 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;
#If you want to remove D or d rows
dat1[!grepl("D|d",dat1$Ch),]
num Ch count
3 3 000000000T00000000000000000000000000000000000000 0.007368;
5 5 000000000T00000000000000000000000000000000000000 0.002456;
7 7 00000000T000000000000000000000000000000000000000 0.007368;
9 9 00000000T000000000000000000000000000000000000000 0.002456;
A.K.
----- Original Message -----
From: Claudia Penaloza <claudiapenaloza at gmail.com>
To: r-help at r-project.org
Cc:
Sent: Monday, July 2, 2012 6:48 PM
Subject: [R] Removing rows if certain elements are found in character string
I would like to remove rows from the following data frame (df) if there are
only two specific elements found in the df$ch character string (I want to
remove rows with only "0" & "D" or "0" & "d"). Alternatively, I would like
to remove rows if the first non-zero element is "D" or "d".
ch count
1 0000000000D0000000000000000000000000000000000000 0.007368;
2 0000000000d0000000000000000000000000000000000000 0.002456;
3 000000000T00000000000000000000000000000000000000 0.007368;
4 000000000TD0000000000000000000000000000000000000 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;
I tried the following but it doesn't work if there is more than one
character per string:
>df <- df[!df$ch %in% c("0","D"),]
>df <- df[!df$ch %in% c("0","d"),]
Any help greatly appreciated,
Claudia
[[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