2

我需要能够在 R 中反转单词。例如将“这是我的文本”转换为“我的文本是这个”。我尝试使用 stringr 包中的 word 函数,如下所示,但没有用,只得到“”

word("this is my text", -1,1)
[1]  "" 

任何建议为什么上述不起作用或任何其他方式来反转单词?

4

1 回答 1

5

我们可以做一个strsplit然后用rev

paste(sapply(strsplit(str1, "\\s+"), rev), collapse= ' ')
#[1] "text my is this"

数据

str1 <- "this is my text"
于 2018-04-27T14:12:23.357 回答