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]]
You should use grepl() instead:
Alu<-mutList[grepl("Alu",mutList)]
or some variant of the above, depending on the actual structure of
mutList (which might be a data frame, but doesn't appear to be a
generic list). str() and dput() are both very useful for asking
well-formed questions.
Sarah
On Fri, Jun 22, 2012 at 12:14 PM, Joshua Budman <josh.budman at gmail.com>
wrote:> 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
>
>
--
Sarah Goslee
http://www.functionaldiversity.org
On Fri, Jun 22, 2012 at 12:30 PM, Joshua Budman <josh.budman at gmail.com> wrote:> Hi, > Thank you very much. You are correct, it is a data frame. However, the code > below is still returning the full data frame. Do you have any suggestions? > Thanks in advance, > Josh > On 22-Jun-12, at 12:22 PM, Sarah Goslee wrote: > > Alu<-mutList[grepl("Alu",mutList)]Ah, a data frame. We need to know that. Since you want to search a specific column for "Alu", you need to tell R that. Alu <- mutList[grep("Alu", mutList$Alu), ] more likely, but in the absence of data provided with dput() I can't test it. Please do read the posting guide. Sarah -- Sarah Goslee http://www.functionaldiversity.org
Hi,
You could 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)
?subset(dat2,grepl("Alu",dat2$Alu))
?????????? Alu
1???????? AluJ
2? AluJ/F(R)AM
4??? AluJ/FLAM
5??? AluJ/FRAM
6 AluJ/monomer
7??????? AluJb
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.
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.