Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split on first comma in string

Tags:

regex

r

How can I efficiently split the following string on the first comma using base?

x <- "I want to split here, though I don't want to split elsewhere, even here."
strsplit(x, ???)

Desired outcome (2 strings):

[[1]]
[1] "I want to split here"   "though I don't want to split elsewhere, even here."

Thank you in advance.

EDIT: Didn't think to mention this. This needs to be able to generalize to a column, vector of strings like this, as in:

y <- c("Here's comma 1, and 2, see?", "Here's 2nd sting, like it, not a lot.")

The outcome can be two columns or one long vector (that I can take every other element of) or a list of stings with each index ([[n]]) having two strings.

Apologies for the lack of clarity.

like image 998
Tyler Rinker Avatar asked Apr 25 '12 03:04

Tyler Rinker


1 Answers

Here's what I'd probably do. It may seem hacky, but since sub() and strsplit() are both vectorized, it will also work smoothly when handed multiple strings.

XX <- "SoMeThInGrIdIcUlOuS"
strsplit(sub(",\\s*", XX, x), XX)
# [[1]]
# [1] "I want to split here"                               
# [2] "though I don't want to split elsewhere, even here."
like image 59
Josh O'Brien Avatar answered Sep 27 '22 20:09

Josh O'Brien