[R] Extract word from string based on pattern match
joeceradini at gmail.com
Mon Oct 24 23:33:20 CEST 2016
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"))
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 :)
If possible, it would also be helpful if something was returned, like
NA, for rows without a "BT" match. So, conceptually, test$x would
NA, "BT-1501-2E", "Bt-1599-3E".
More information about the R-help