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)
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