如何反转单词?

Cur*_*us 2 r stringr

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

word("this is my text", -1,1)
[1]  "" 
Run Code Online (Sandbox Code Playgroud)

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

akr*_*run 5

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

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

数据

str1 <- "this is my text"
Run Code Online (Sandbox Code Playgroud)