但是应该有一些漂亮的方法来做到这一点,一种方法是拆分每个单词并降低除第一个单词之外的单词的所有字符,然后将 paste 字符串返回。
paste0(sapply(strsplit(text, " ")[[1]], function(x)
paste0(substr(x, 1, 1),tolower(substr(x, 2, nchar(x))))), collapse = " ")
#[1] "This String Is a test. Trying To find a way to do This."
详细的分步说明:
strsplit(text, " ")[[1]]
#[1] "This" "String" "IS" "a" "tESt." "TRYING" "TO" "fINd"
# [9] "a" "waY" "to" "do" "ThiS."
sapply(strsplit(text, " ")[[1]], function(x)
paste0(substr(x, 1, 1),tolower(substr(x, 2, nchar(x)))))
# This String IS a tESt. TRYING TO fINd
# "This" "String" "Is" "a" "test." "Trying" "To" "find"
# a waY to do ThiS.
# "a" "way" "to" "do" "This."
paste0(sapply(strsplit(text, " ")[[1]], function(x)
paste0(substr(x, 1, 1),tolower(substr(x, 2, nchar(x))))), collapse = " ")
#[1] "This String Is a test. Trying To find a way to do This."