I want to split this vector
c("CC", "C/C")
to
[[1]]
[1] "C" "C"
[[2]]
[1] "C" "C"
My final data should look like:
c("C_C", "C_C")
Thus, I need some regex
, but don't found how to solve the "non-space" part:
strsplit(c("CC", "C/C"),"|/")
You can use sub
(or gsub
if it occurs more than once in your string) to directly replace either nothing or a forward slash with an underscore (capturing one character words around):
sub("(\\w)(|/)(\\w)", "\\1_\\3", c("CC", "C/C"))
#[1] "C_C" "C_C"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With