Extracting rows on basis of specific column values in R
1
0
Entering edit mode
4.3 years ago

I have a dataframe that contains various fields. An instance being:

Protein Code Sequence CODON XYZ L AATTGCTA MET ABC K ATTGCTA MET-2 JKL B GCTTATA LYS-2

I want to extract information of all the elements in which the column CODON ends with "-2".

Like in this case i get : ABC K ATTGCTA MET-2 JKL B GCTTATA LYS-2

Please suggest the answer.

I used :

od_lig <- filter(odor_lig, CODON== "$-2")

But I got blank data.

R PYTHON DATASETS DATAFRAME • 1.3k views
ADD COMMENT
0
Entering edit mode

Because you are using a regular expression and filter doesn't understand regex directly. Wrap your regex in grepl and also give it the target column and wrap that in your filter function.

ADD REPLY
0
Entering edit mode

please work through any of the many online R tutorials. You could also look up "how to ask questions" and unfortunately it looks like a correct tagging of posts tutorial is also necessary. Maybe you think the $ would signify a regex end matching? try endsWith

ADD REPLY
0
Entering edit mode

What does a value in the CODON column actually look like?

ADD REPLY
1
Entering edit mode
4.3 years ago
Shalu Jhanwar ▴ 540

Suppose the entire protein Code Sequence is read in one column (codon) of the data frame. You can use the below code in R script to extract rows ends with "-2":

grepl("-2$",df$codon)
ADD COMMENT
0
Entering edit mode

Thank you so much for your help

ADD REPLY
1
Entering edit mode

If an answer was helpful, you should upvote it; if the answer resolved your question, you should mark it as accepted. You can accept more than one answer if they work. This will help future users that might find this post find the right answer.

Upvote|Bookmark|Accept

ADD REPLY

Login before adding your answer.

Traffic: 2468 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6