strng <- "so [usualmente] maybe [me levanto como a las nueve y media] like I exercise and the I like either go to class online or in person like it depends on the day"
代码
strng <- "so [usualmente] maybe [me levanto como a las nueve y media] like I exercise and the I like either go to class online or in person like it depends on the day"
vecSeq <- Vectorize(seq.default, vectorize.args = c("to", "from"))
ixstart <- grep("\\[", unlist(strsplit(strng, " ")))
ixend <- grep("\\]", unlist(strsplit(strng, " ")))
spanish_ix <- unlist(vecSeq(ixstart, ixend, 1))
english_ix <- setdiff(1:(lengths(gregexpr("\\W+", strng)) + 1), spanish_ix)
spanish <- paste(stringr::word(strng, spanish_ix), collapse = " ")
english <- paste(stringr::word(strng, english_ix), collapse = " ")
#spanish
#[1] "[usualmente] [me levanto como a las nueve y media]"
#> english
#[1] "so maybe like I exercise and the I like either go to class #online or in person like it depends on the day"
1条答案
按热度按时间0x6upsns1#
你可以通过
Vectorize
调用seq
函数并建立索引,然后使用stringr::word
提取索引处的整个单词来实现这一点:示例字符串:
代码
注意,要删除讨厌的括号只是做:
spanish <- gsub("\\]|\\[", "", spanish)