| is a metacharacter. You need to avoid this (using \\ before it).
> unlist(strsplit("I am | very smart", " \\| ")) [1] "I am" "very smart" > sub(pattern="\\|", replacement="*", x="I am | very smart") [1] "I am * very smart"
Edit: The reason you need two backslashes is because one backslash prefix is ββreserved for special characters such as \n (new line) and \t (tab). See the ?regex page for more information. Other metacharacters:. . \ | ( ) [ { ^ $ * + ?
source share