如何倒转单词?

How to reverse words?

我需要能够在 R 中反转单词。例如将 "this is my text" 转换为 "text my is this"。我尝试使用 stringr 包中的 word 函数,如下所示,但没有用,只得到 ""

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

关于为什么上述方法不起作用或任何其他反转单词的方法有什么建议吗?

我们可以做一个 strsplit 然后 rev

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

数据

str1 <- "this is my text"