R split on delimiter (split) keep the delimiter (split)
Asked Answered
K

1

26

In R you can use the strsplit function to split a vector on a delimiter(split) as follows:

x <- "What is this?  It's an onion.  What! That's| Well Crazy."
unlist(strsplit(x, "[\\?\\.\\!\\|]", perl=TRUE))

## [1] "What is this"    "  It's an onion" "  What"          " That's"        
## [5] " Well Crazy"

I'd like to keep the delimiter(split) using R. So the desired output would be:

## [1] "What is this?"    "  It's an onion." "  What!"          " That's|"        
## [5] " Well Crazy."
Kalvn answered 1/2, 2014 at 1:38 Comment(0)
K
28

You can use "(?<=DELIMITERS)":

unlist(strsplit(x, "(?<=[?.!|])", perl=TRUE))

## [1] "What is this?"    "  It's an onion." "  What!"          " That's|"        
## [5] " Well Crazy.
Kalvn answered 1/2, 2014 at 1:39 Comment(2)
You don't need all of the backslashes. unlist(strsplit(x, "(?<=[?.!|])", perl=TRUE)) returns the same resultPerforce
Would love an option where the split is a sequence (e.g. "[0-9]+") rather than a single character...Foxe

© 2022 - 2024 — McMap. All rights reserved.