3
> test = "23*45"

I'd like to split testby the symbol *

I tried...

> strsplit(test,'*')

and I got...

[[1]]
[1] "2" "3" "*" "4" "5"

What I aim to have is:

[[1]]
[1] "23" "45"
4

2 回答 2

5

你需要逃离星星...

test = "23*45"

strsplit( test , "\\*" )
#[[1]]
#[1] "23" "45"

split是一个正则表达式,*表示前面的项目匹配零次或多次。如. _ _ 表示 *treat作为文字。strsplit()\\***

或者使用fixed参数...

strsplit( test , "*" , fixed = TRUE )
#[[1]]
#[1] "23" "45"

这使 R 将拆分模式视为文字而不是正则表达式。

于 2013-11-05T12:00:01.460 回答
3

你可能想看看这个包: http ://www.rexamine.com/resources/stringi/

要安装此软件包,只需运行:

install.packages("stringi")

例子:

stri_split_fixed(test, "*")
于 2013-11-05T11:58:31.797 回答