Extracting rows on basis of specific column values in R
1
0
Entering edit mode
22 months 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

I used :

od_lig <- filter(odor_lig, CODON== "$-2")  But I got blank data. R PYTHON DATASETS DATAFRAME • 551 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

0
Entering edit mode

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

1
Entering edit mode
22 months ago
Shalu Jhanwar ▴ 500

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)

0
Entering edit mode

Thank you so much for your help

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.