Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove strings that contain two or more capital letters from a vector in r

Tags:

r

I have the following vector for an example.

isotopes <- c("6Li", "7Li", "7LiH", "10B", "11B", "11BH")

I want to remove the strings "7LiH" and "11BH" from the vector. These values have two capital letters and so I am trying to figure out how to use grep to remove those values or just index out the other strings in the vector. How can I do this?

like image 909
ZT_Geo Avatar asked Sep 05 '25 03:09

ZT_Geo


1 Answers

You can simply grep for elements that contain 2 or more capital letters and invert the match:

grep('[A-Z].*[A-Z]', isotopes, value=TRUE, invert=TRUE)

The regex matches a string that contains an uppercase letter, then probably something else and then one more uppercase letter (not necessary at the beginning or end)

like image 78
STerliakov Avatar answered Sep 07 '25 16:09

STerliakov