我需要能够在 R 中反转单词。例如,将“这是我的文本”转换为“我的文本是这个”。我尝试使用 stringr 包中的 word 函数,如下所示,但没有用,只有“”
word("this is my text", -1,1)
[1] ""
Run Code Online (Sandbox Code Playgroud)
任何建议为什么上述方法不起作用或任何其他方式来反转单词?
我们可以做一个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)