繁体   English   中英

R-使用正则表达式和ifelse条件从字符串中分离文本时出错

[英]R - error in separating text from a string using regex and ifelse condition

我想做的是从有“:”的地方从字符串中剥离文本。

假设我的文字包含:

 text$Text[[3]] = "There is a horror movie running in the iNox theater. : Can we go?"

我想要创建一个数据框,例如:

  Col1                                                    Col2
  There is a horror movie running in the iNox theater.    Can we go?

我正在尝试使用以下内容:

 df = data.frame(Text = strsplit(text$Text[[3]], 
                 ifelse(":", ":", text$Text[[3]]))[[1]], stringsAsFactors = F)

dat3$Text[[3]]因为文本在行号中。 文本$ Text中的3。

但是上述ifelse()逻辑无效。 在这里,我尝试使用ifelse条件,以便如果文本中包含“:”,请使用“:”,否则请使用完整的文本。 因此,这意味着如果没有“:”,则结果将类似于以下内容:

 text$Text[[3]] = "Hi Mom, You there. Can I go to Jimmy's house?"

 Col1                                                 Col2
 Hi Mom, You there. Can I go to Jimmy's house?         NA

如何正确做?

请注意有一个陷阱:

  • 如果文本中有两个“:”怎么办?
  • 我只想考虑在前两行中而不是在文本其余部分中的“:”?

我发现以下内容太复杂了,比我更了解正则表达式的人一定会提出更好的解决方案。

test <- c(
"There is a horror movie running in the iNox theater. : Can we go?",
"Hi Mom, You there. Can I go to Jimmy's house?",
"Hi : How are you : Lets go")

fun <- function(x, pattern = ":"){
    re <- regexpr(pattern, x)
    res <- sapply(seq_along(re), function(i){
        if(re[i] > 0){
            Col1 <- trimws(substring(x[i], 1, re[i] - 1))
            Col2 <- trimws(substring(x[i], re[i] + 1))
        } else {
            Col1 <- x[i]
            Col2 <- NA
        }
        c(Col1 = Col1, Col2 = Col2)
    })
    as.data.frame(t(res))
}

fun(test)

您实际上不需要if语句。 正则表达式旨在处理此类情况。

对于只有一个符号的数据的第一种情况-在此示例中为冒号(“:”)–我们可以使用以下代码:

x <- "There is a horror movie running in the iNox theater. : Can we go?"

data.frame(Col1=gsub("(.*)+\\s[:]\\s+(.*)","\\1",x), 
           Col2=gsub("(.*)+\\s[:]\\s+(.*)","\\2",x))

输出:

                                                  Col1            Col2
1 There is a horror movie running in the iNox theater.      Can we go?

现在,假设您的字符串中有多个符号,并且希望能够将信息保留在第一列的第一个符号之前,并将信息保留在第二列的第一个符号之后。 为此,请尝试使用“?” 正则表达式符号,如下所示:

x <- "There is a horror movie running in the iNox theater. : Can we go? : Please?"

data.frame(Col1=gsub("\\s\\:.*$","\\1",x), 
           Col2=gsub("^[^:]+(?:).\\s","\\1",x))

输出:

                                                  Col1                      Col2
1 There is a horror movie running in the iNox theater.      Can we go? : Please?

有关在R中使用正则表达式符号的更多信息, 这是一个有用的参考

test <- "There is a horror movie running in the iNox theater. : Can we go?"
df = data.frame(Col1 = strsplit(test,":")[[1]][1],
                Col2 = strsplit(test,":")[[1]][2],
                stringsAsFactors = F)
df
#                                                   Col1        Col2
#1 There is a horror movie running in the iNox theater.   Can we go?

请注意,strsplit()输出的异常第一行由[[1]]组成。 与[R]显示向量的方式类似,[[1]]表示R正在显示列表的第一个元素。

您可以使用包纵梁

library(stringr) 
str_split_fixed("HI : How are you : Lets go", ":", 3)

在上面的函数str_split_fixed中, “嗨:您好:如何放手”是您要使用的句子或字符串, “:”是字符串中的分隔符,而3是您希望将字符串拆分为的列数

在您的情况下,最后一个值应为2,因为您想分成两列

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM