R 中多单词字符串中的第一个单词大写

问题描述 投票:0回答:1

在 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')
r string text
1个回答
0
投票
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"
© www.soinside.com 2019 - 2024. All rights reserved.