R strsplit 不拆分“。”?

R strsplit doesn't split on "."?

我正在编写一个 R 脚本,并希望定义一个变量以作为文件名的一部分在绘图注释中使用。我想我会使用 strsplit() 函数。这是我的代码和输出:

infile = "ACC_1346.table.txt"

x = strsplit(infile, ".")

class(infile)
[1] "character"

class(x)
[1] "list"

str(x)
List of 1
$ : chr [1:18] "" "" "" "" ...

x[[1]]
[1] "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""

我预计最终输出为:

[1] "ACC_1346" "table" "txt"

这是怎么回事?

strsplit 寻找一个 regex to do it's splitting. In regex a "." 是一个几乎可以匹配任何东西的通配符。要实际匹配您需要使用 \ 转义的点。由于 \ 也是R中的转义字符,因此需要将其转义两次 \.

为了完全避免正则表达式,在 strsplit

的调用中使用 fixed = TRUE
infile = "ACC_1346.table.txt"
x = strsplit(infile, ".", fixed = TRUE)

x

[[1]]
[1] "ACC_1346" "table" "txt"