This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Extracting rows on basis of specific column values in R

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

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.

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

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

1 answer

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)

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

Log in to answer this question.