Separate a line with a plus sign (+)

I have a line in a data frame: "(1)+(2)"

I want to separate with the separator "+", so that I get one element like the (1)rest like (2), therefore, keeping the parentheses. I used strsplit, but it does not save the brackets.

+4
source share
1 answer

Using

strsplit("(1)+(2)", "\\+")

or

strsplit("(1)+(2)", "+", fixed = TRUE)

The idea of โ€‹โ€‹using strsplit("(1)+(2)", "+")does not work, because unless otherwise specified, the argument splitis a regular expression, and the character +is special in regex. Other characters who also need special care,

  • ?
  • *
  • .
  • ^
  • $
  • \
  • |
  • { }
  • [ ]
  • ( )
+7
source

Source: https://habr.com/ru/post/1598798/


All Articles