Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to make grep pull only exact character string matches in r

Basically, I’m trying to do the same thing a lot of other people have posted about in Unix (like How to make grep only match if the entire line matches?), but for using the grep() function in R.

so, say I have a data.frame df with rownames:

              Var_1       Var_2       Var_3
sample.1       1           1.5         1.3
sample.1.2     2.5         3.2         1.7
sample.2       1.8         2.2         3.4
sample.2.5     3.3         3.1         2.8

and I want to pull the sample.1 row but NOT sample.1.2 (but in actuality iterated over thousands of columns so I can’t just specify not sample.1.2)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I would use df[grep("sample.1", rownames(df)),] , but that would also return sample.1.2, as does setting fixed = TRUE. Trying to grep "sample.1 " instead also doesn’t work because there is no space character used in the data.frame.

How would I go about this?

>Solution :

Use the end of string anchor ($) which states that there should be nothing after the string. As stated in the comments, you can also use ^ (start of string anchor) to ensure an exact match.

df[grep("sample.1$", rownames(df)),]
         Var_1 Var_2 Var_3
sample.1     1   1.5   1.3
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading