用“|”分隔字符串文本不起作用

separating string text by "|" doesn't work

我有一个变量,它的值是字符串形式,看起来像这样:

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

当我使用这个命令时:

strsplit(df$var,"|")

我得到以下输出:

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

这让我觉得语法有问题。如果有人能指出问题出在哪里,我们将不胜感激?

如果你想移除JavaScript中的管道,你可以这样做:

let str = "test_intro|test_wm02|test_wf06|test_lf10|t ....";
str.split("|");
// returns an array of your string broken up, without the pipe`

您需要指定 fixedTRUE:

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

输出:

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

如果 fixed 是默认值 (FALSE),则 split 表达式将被视为 正则表达式 。相反,您想按确切字符 | 拆分,因此 fixed 必须是 TRUE.

在此处添加更可靠的答案,因为 fixed = TRUE 可能会解决此问题,但可能会导致其他问题。这里的问题是 | 字符在 Regex 中表示“或”。所以你是说将字符串拆分为空白或空白。在空白处拆分是 strsplit 中的一项特殊功能,它有意将字符串分成其字符组成部分(这有时非常有用)。

您可以不使用 fixed = TRUE 参数,而是以 Regex 格式编写拆分字符。在 R 中,这意味着您将需要双重转义。

test <- "test_intro|test_wm02|test_wf06|test_lf10|t ...."

# The following doesn't work as expected because | is an or character in regex.
strsplit(test,"|")
# [1] "t" "e" "s" "t" "_" "i" "n" "t" "r" "o" "|" "t" "e" "s" "t" "_" "w" "m" "0" "2" "|" "t" "e" "s" "t" "_" "w" "f" "0"
# [30] "6" "|" "t" "e" "s" "t" "_" "l" "f" "1" "0" "|" "t" " " "." "." "." "."

# Escaping the | character (see regex manual) will make the code work as expected
strsplit(test,"\|")
# [1]  "test_intro" "test_wm02"  "test_wf06"  "test_lf10"  "t ...."