Search code examples
rstringstrsplit

separating string text by "|" doesn't work


I have a variable, whose value is in string form and looks like this:

test_intro|test_wm02|test_wf06|test_lf10|t ....

When I use this command:

strsplit(df$var,"|")

I get the following output:

"t" "e" "s" "t" "_" "i" "n" "t" "r" "o" "|" "t" "e" "s" "t" "_" "w" "m" "0" "1" "|" "t" "e ....

which makes me think that there's something wrong with the syntax. Would appreciate if someone could point to where the problem might be?


Solution

  • You need to specify that fixed is TRUE:

    strsplit(df$var, "|", TRUE)
    

    Output:

    "test_intro" "test_wm02"  "test_wf06"  "test_lf10"  "t ...."  
    

    If fixed is default (FALSE) then the split expression will be treated as a regular expression. Instead, you want to split by the exact character |, so fixed must be TRUE.