基于R中的点分割文本[重复]

时间:2022-11-28 21:38:53

This question already has an answer here:

这个问题在这里已有答案:

I have:

我有:

"word1.word2"

and I want:

而且我要:

"word1" "word2"

I know I have to use strsplit with perl=TRUE, but I can't find the regular expression for a period (to feed to the split argument).

我知道我必须使用strsplit和perl = TRUE,但我找不到一段时间的正则表达式(以反馈split参数)。

2 个解决方案

#1


8  

There are several ways to do this, both with base R and with the common string processing packages (like "stringr" and "stringi").

有几种方法可以做到这一点,包括基本R和常见的字符串处理包(如“stringr”和“stringi”)。

Here are a few in base R:

以下是基数R中的一些:

str1 <- "word1.word2"

strsplit(str1, ".", fixed = TRUE)  ## Add fixed = TRUE
strsplit(str1, "[.]")              ## Make use of character classes
strsplit(str1, "\\.")              ## Escape special characters 

#2


3  

Try this

尝试这个

library(stringr)
a <- "word1.word2"
str_split(a, "\\.")

#1


8  

There are several ways to do this, both with base R and with the common string processing packages (like "stringr" and "stringi").

有几种方法可以做到这一点,包括基本R和常见的字符串处理包(如“stringr”和“stringi”)。

Here are a few in base R:

以下是基数R中的一些:

str1 <- "word1.word2"

strsplit(str1, ".", fixed = TRUE)  ## Add fixed = TRUE
strsplit(str1, "[.]")              ## Make use of character classes
strsplit(str1, "\\.")              ## Escape special characters 

#2


3  

Try this

尝试这个

library(stringr)
a <- "word1.word2"
str_split(a, "\\.")