I was curious about:
> 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="|")?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It’s because the
splitargument is interpreted as a regular expression, and|is a special character in a regex.To get round this, you have two options:
Option 1: Escape the
|, i.e.split = "\\|"Option 2: Specify
fixed = TRUE:Please also note the See Also section of
?strsplit, which tells you to read?"regular expression"for details of the pattern specification.