How to Reverse a string in R

From ?strsplit, a function that’ll reverse every string in a vector of strings:

## a useful function: rev() for strings
strReverse <- function(x)
        sapply(lapply(strsplit(x, NULL), rev), paste, collapse="")
strReverse(c("abc", "Statistics"))
# [1] "cba"        "scitsitatS"

Leave a Comment