R 中的 strsplit 使用 |作为模式
strsplit in R using | as the pattern
我有一系列字符串需要根据字符“|”进行拆分例如:
fruits <- c(
"apples|oranges|pears|bananas",
"pineapples|mangos|guavas"
)
str_split(fruits, "|")
输出:
[[1]]
[1] "" "a" "p" "p" "l" "e" "s" "|" "o" "r" "a" "n" "g" "e" "s" "|"
[17] "p" "e" "a" "r" "s" "|" "b" "a" "n" "a" "n" "a" "s" ""
[[2]]
[1] "" "p" "i" "n" "e" "a" "p" "p" "l" "e" "s" "|" "m" "a" "n" "g"
[17] "o" "s" "|" "g" "u" "a" "v" "a" "s" ""
这不是我要找的,我要的是
[[1]]
[1] "apples" "oranges" "pears" "bananas"
[[2]]
[1] "pineapples" "mangos" "guavas"
我认为我在 str_split 的 RegEx 模式参数中输入的内容有问题,但我不确定它有什么问题。
请帮忙,谢谢。
我需要转义“|”
str_split(fruits, "\|")
我有一系列字符串需要根据字符“|”进行拆分例如:
fruits <- c(
"apples|oranges|pears|bananas",
"pineapples|mangos|guavas"
)
str_split(fruits, "|")
输出:
[[1]]
[1] "" "a" "p" "p" "l" "e" "s" "|" "o" "r" "a" "n" "g" "e" "s" "|"
[17] "p" "e" "a" "r" "s" "|" "b" "a" "n" "a" "n" "a" "s" ""
[[2]]
[1] "" "p" "i" "n" "e" "a" "p" "p" "l" "e" "s" "|" "m" "a" "n" "g"
[17] "o" "s" "|" "g" "u" "a" "v" "a" "s" ""
这不是我要找的,我要的是
[[1]]
[1] "apples" "oranges" "pears" "bananas"
[[2]]
[1] "pineapples" "mangos" "guavas"
我认为我在 str_split 的 RegEx 模式参数中输入的内容有问题,但我不确定它有什么问题。
请帮忙,谢谢。
我需要转义“|”
str_split(fruits, "\|")