How to reverse a sentence in R?
问题 I want a function that takes a string (NOT a vector) and reverses the words in that string. For example, rev_sentence("hi i'm five") ## [1] "five i'm hi" I have a function that reverses individual characters, but not something that will reverse a string that's essentially a sentence. 回答1: In R , We can use strsplit to split at one or more spaces and then reverse the elements and paste it together sapply(strsplit(str1, "\\s+"), function(x) paste(rev(x), collapse=" ")) #[1] "five i'm hi" If