Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I split and retain elements using strsplit?

Tags:

r

strsplit

What a strsplit function in R does is, match and delete a given regular expression to split the rest of the string into vectors.

>strsplit("abc123def", "[0-9]+")
[[1]]
[1] "abc" ""    ""    "def" 

But how should I split the string the same way using regular expression, but also retain the matches? I need something like the following.

>FUNCTION("abc123def", "[0-9]+")
[[1]]
[1] "abc" "123" "def" 

Using strapply("abc123def", "[0-9]+|[a-z]+") works here, but what if the rest of the string other than the matches cannot be captured by a regular expression?

like image 858
jackson Avatar asked Jun 13 '12 11:06

jackson


People also ask

What is a function of STR split?

The strsplit() in R programming language function is used to split the elements of the specified character vector into substrings according to the given substring taken as its parameter.

What is str_ split R?

The str_split() function from the stringr package in R can be used to split a string into multiple pieces. This function uses the following syntax: str_split(string, pattern) where: string: Character vector.


2 Answers

Fundamentally, it seems to me that what you want is not to split on [0-9]+ but to split on the transition between [0-9]+ and everything else. In your string, that transition is not pre-existing. To insert it, you could pre-process with gsub and back-referencing:

test <- "abc123def"
strsplit( gsub("([0-9]+)","~\\1~",test), "~" )

[[1]]
[1] "abc" "123" "def"
like image 88
Ari B. Friedman Avatar answered Sep 26 '22 18:09

Ari B. Friedman


You could use lookaround assertions.

> test <- "abc123def"
> strsplit(test, "(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)", perl=T)
[[1]]
[1] "abc" "123" "def"
like image 38
Avinash Raj Avatar answered Sep 24 '22 18:09

Avinash Raj