[R] Search list of elements for a specific pattern

arun smartpink111 at yahoo.com
Fri Jun 22 23:00:18 CEST 2012


Hi,

You could also use "grep" to get the same result.

Try this:
dat2<-read.table(text="
     Alu
1        AluJ
2  AluJ/F(R)AM
3     monomer
4    AluJ/FLAM
5    AluJ/FRAM
6 AluJ/monomer
7        AluJb
8        JBF
9       FRAM
",sep="",header=TRUE)


#grepl     
dat3<-subset(dat2,grepl("Alu",dat2$Alu))
> dat3
           Alu
1         AluJ
2  AluJ/F(R)AM
4    AluJ/FLAM
5    AluJ/FRAM
6 AluJ/monomer
7        AluJb


#using grep
dat4<-data.frame(subset(dat2$Alu,dat2$Alu %in% grep("^Alu", dat2$Alu, value=TRUE),dat2))
colnames(dat4)<-"Alu"
> dat4
           Alu
1         AluJ
2  AluJ/F(R)AM
3    AluJ/FLAM
4    AluJ/FRAM
5 AluJ/monomer
6        AluJb


#The difference is in the row numbers.  The row numbers in "grepl" correspond to the actual row numbers in the original file.
#You can change this to the correct order by,

row.names(dat3)<-1:nrow(dat3)



A.K.







----- Original Message -----
From: Joshua Budman <josh.budman at gmail.com>
To: R-help at r-project.org
Cc: 
Sent: Friday, June 22, 2012 12:14 PM
Subject: [R] Search list of elements for a specific pattern

Hi,
I have a list of mutations, called "mutList", of the form:

> head(mutList)
            Alu
1         AluJ
2  AluJ/F(R)AM
3    AluJ/FLAM
4    AluJ/FRAM
5 AluJ/monomer
6        AluJb

It contains about 500 elements and not all of them contain the  
sequence "Alu". I tried using this code:
Alu<-mutList[which(grep("Alu",mutList)==1)]

But that simply returned all of them elements in the list. Is there a  
way to modify the list such that I have only the elements containing  
"Alu" in the new list? Help would be appreciated!

Josh


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