[R] Extract word from string based on pattern match
Joe Ceradini
joeceradini at gmail.com
Mon Oct 24 23:33:20 CEST 2016
R Helpers,
I would like to extract the entire word beginning with "BT" (or "BT-")
and not any thing else in the string. Or, I would like to extract from
BT up until the next space.
test <- data.frame(x = c("abc", "Sample BT-1501-2E stuff", "Bt-1599-3E stuff"))
test
So, from test$x I would like to only extract "BT-1501-2E" and "Bt-1599-3E".
I started with straight grep but of course that is not what I need.
grep("BT", test$x, value = TRUE, ignore.case = TRUE)
"Sample BT-1501-2E stuff" "Bt-2134df stuff"
In a somewhat similar post, the solution involved boundaries or
anchors, but I haven't been able to adapt it to my needs, so I won't
even bother including my boundary attempts :)
http://stackoverflow.com/questions/7227976/using-grep-in-r-to-find-strings-as-whole-words-but-not-strings-as-part-of-words
If possible, it would also be helpful if something was returned, like
NA, for rows without a "BT" match. So, conceptually, test$x would
return:
NA, "BT-1501-2E", "Bt-1599-3E".
Thanks!
Joe
More information about the R-help
mailing list