Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split string according to commas in R

Tags:

text

r

strsplit

I have the following:

s <- "abc, xyz, poi (cv, r2, 44, rghj), wer"

How can I split it so the end result is:

c("abc", "xyz", "poi (cv, r2, 44, rghj)", "wer")

Basically, strsplit the string at every comma, but outside the parentheses.

like image 415
dimitris_ps Avatar asked Mar 24 '26 00:03

dimitris_ps


1 Answers

Try

strsplit(s, "\\([^)]+\\)(*SKIP)(*FAIL)|, ", perl = TRUE)[[1]]
#[1] "abc"                    "xyz" 
#[3] "poi (cv, r2, 44, rghj)" "wer"        
like image 183
akrun Avatar answered Mar 25 '26 13:03

akrun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!