在 R 中,如何仅将单词和多词字符串向量中每个字符串中的第一个单词大写? 我找到的解决方案(stringr::str_to_sentence()、stringr::str_to_title(),将字符串中的每个单词大写。
谢谢!
strings <- c('one', 'one two', 'three', 'five six')
expected.result <- c('One', 'One two', 'Three four five', 'Six seven')
stringr::str_to_sentence(strings)
[1] "One" "One two" "Three" "Five six"
基础R:
sub("(\\w)", "\\U\\1", strings, perl = TRUE)
[1] "One" "One two" "Three" "Five six"