Home > front end >  Split string in parts by minus and plus in R
Split string in parts by minus and plus in R

Time:06-06

I want to split this string: test = "-1x^2 3x^3-x^8 1-x" ...into parts by plus and minus characters in R. My goal would be to get: "-1x^2" " 3x^3" "-x^8" " 1" "-x"

This didn't work:

strsplit(test, split = "-")
strsplit(test, split = " ")

CodePudding user response:

We can provide a regular expression in strsplit, where we use ?= to lookahead to find the plus or minus sign, then split on that character. This will allow for the character itself to be retained rather than being dropped in the split.

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

# [1] "-1x^2" " 3x^3" "-x^8"  " 1"    "-x"   

CodePudding user response:

Try

> strsplit(test, split = "(?<=.)(?=[ -])", perl = TRUE)[[1]]
[1] "-1x^2" " 3x^3" "-x^8"  " 1"    "-x"

where (?<=.)(?=[ -]) captures the spliter that happens to be in front of or -.

CodePudding user response:

This uses gsub to search for any character followed by or - and inserts a semicolon between the two characters. Then it splits on semicolon.

s <- "-1x^2 3x^3-x^8 1-x"
strsplit(gsub("(.)([ -])", "\\1;\\2", s), ";")[[1]]
## [1] "-1x^2" " 3x^3" "-x^8"  " 1"    "-x"   
  • Related