strsplit("ty,rr", split = ",") [[1]] [1] "ty" "rr" > strsplit...">

Symbol "|" in strsplit function (vertical strip / pipe)

It was interesting to me:

> strsplit("ty,rr", split = ",") [[1]] [1] "ty" "rr" > strsplit("ty|rr", split = "|") [[1]] [1] "t" "y" "|" "r" "r" 

Why don't I get c("ty","rr") from strsplit("ty|rr", split="|") ?

+4
source share
1 answer

This is because the split argument is interpreted as a regular expression, and | - a special character in regular expression.

To get around this, you have two options:

Option 1: Escape | , i.e. split = "\\|"

 strsplit("ty|rr", split = "\\|") [[1]] [1] "ty" "rr" 

Option 2: Specify fixed = TRUE :

 strsplit("ty|rr", split = "|", fixed = TRUE) [[1]] [1] "ty" "rr" 

Also see the See Also section ?strsplit , which says what you read ?"regular expression" for more information on the template specification.

+13
source

All Articles