具有多个条件的R strsplit()
我试图根据不同的标准来划分句子。我希望在“是”之后拆分一些句子,在“从不”之后拆分一些句子。我可以根据这两种情况中的任何一种来分句,但不能同时基于这两种情况具有多个条件的R strsplit(),r,R,我试图根据不同的标准来划分句子。我希望在“是”之后拆分一些句子,在“从不”之后拆分一些句子。我可以根据这两种情况中的任何一种来分句,但不能同时基于这两种情况 str <- matrix(c("This is line one", "This is not line one", "This can never be line one"), nrow = 3, ncol = 1) >str [,1]
str <- matrix(c("This is line one", "This is not line one",
"This can never be line one"), nrow = 3, ncol = 1)
>str
[,1]
[1,] "This is line one"
[2,] "This is not line one"
[3,] "This can never be line one"
str2 <- apply(str, 1, function (x) strsplit(x, " is", fixed = TRUE))
> str2
[[1]]
[[1]][[1]]
[1] "This" " line one"
[[2]]
[[2]][[1]]
[1] "This" " not line one"
[[3]]
[[3]][[1]]
[1] "This can never be line one"
str
[,1]
[1,]“这是第一行”
[2,]“这不是第一行”
[3,]“这永远不会是第一行”
str2 str2
[[1]]
[[1]][[1]]
[1] 这是“第一行”
[[2]]
[[2]][[1]]
[1] 此“”不是第一行
[[3]]
[[3]][[1]]
[1] “这永远不会是第一行”
我想把最后一句话分拆在“从不”之后。我不知道怎么做。我们可以使用regex lookarounds在“is”或“never”后面的空格处拆分行。在这里,
(?FYIstrsplit
是矢量化的。不需要apply
Mabyestrsplit(x,“is | never”)
?@akrun我要说的是它可能是重复的,基本上这两个问题都想在regex中使用OR运算符。另外,链接相关帖子也很好。@akrun帖子甚至没有标记regex,“is”和“is”“从来没有“都是固定词。我们显然有不同的阈值来接受一篇文章作为复制品,让我们就这样吧。对不起,复制品的链接与此无关。所以,重新打开它。你能添加一些关于你答案的详细信息吗?这似乎很复杂,因为\\@ali为第一个案例添加了一些详细信息。谢谢。我已经是第三个了:)
strsplit(str[,1], "(?<=\\bis)\\s+|(?<=\\bnever)\\s+", perl = TRUE)
#[[1]]
#[1] "This is" "line one"
#[[2]]
#[1] "This is" "not line one"
#[[3]]
#[1] "This can never" "be line one"
strsplit(str[,1], "(?:\\s+(is|never)\\s+)")
#[[1]]
#[1] "This" "line one"
#[[2]]
#[1] "This" "not line one"
#[[3]]
#[1] "This can" "be line one"