Another option avoiding string subsetting/splitting is to convert the string to an integer vector to reverse the order so that we can use stringr::str_to_title
.
library(stringr)
library(dplyr)
mycap <- function(mystr = "") {
mystr %>% utf8ToInt %>% rev %>% intToUtf8 %>% str_to_title %>% utf8ToInt %>% rev %>% intToUtf8
}
mycap("MarsuPial")
#[1] "marsupiaL"
mycap("dummy")
#[1] "dummY"
Or another fast option is to use stringi::stri_reverse
and stringi::stri_trans_totitle
library(stringi)
mycap <- function(mystr = "") stri_reverse(stri_trans_totitle(stri_reverse(mystr)))