0

I have column sr$classf in sr dataframe with string like hierarchy servicegroup\service\servicerequest\. I want to split string with strsplit() by \ character and put different parts of splitted string in additional columns. For example I create sr.c7$service column ans put first part before \

 sr$service <- as.character(lapply(strsplit(as.character(sr$classf), split="\"), "[", 1))

But R comment out all code following split="\". What should I do to use \ as a split parameter?

Solution:

 sr$service <- as.character(lapply(strsplit(as.character(sr$classf), split="\\\\"), "[", 1))

1 Answers1

1

Use four backslashes to match a single backslash character.

strsplit("foo\\bar", "\\\\")[[1]]
#[1] "foo" "bar"
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274