用两个字的string大写两个单词的首字母
假设我有两个字的string,我想把它们两个都大写。
name <- c("zip code", "state", "final count")   Hmisc包中有一个capitalize大写的函数,但我不确定如何得到大写的第二个单词。  capitalize的帮助页面并不意味着它可以执行该任务。 
 library(Hmisc) capitalize(name) # [1] "Zip code" "State" "Final count" 
我想得到:
 c("Zip Code", "State", "Final Count") 
三字串的情况如何:
 name2 <- c("I like pizza") 
	
 执行大写的基本R函数是toupper(x) 。 从?toupper的帮助文件中,有这个function可以完成你所需要的function: 
 simpleCap <- function(x) { s <- strsplit(x, " ")[[1]] paste(toupper(substring(s, 1,1)), substring(s, 2), sep="", collapse=" ") } name <- c("zip code", "state", "final count") sapply(name, simpleCap) zip code state final count "Zip Code" "State" "Final Count" 
编辑这适用于任何string,无论字数:
 simpleCap("I like pizza a lot") [1] "I Like Pizza A Lot" 
 匹配一个正则expression式,该expression式从开始^或之后的空格[[:space:]] ,后跟一个字母字符[[:alpha:]] 。 在全球范围内(g中的g)用匹配的开始或空格和匹配的字母字符的大写版本replace所有这样的事件, \\1\\U\\2 。 这必须用Perl风格的正则expression式匹配来完成。 
 gsub("(^|[[:space:]])([[:alpha:]])", "\\1\\U\\2", name, perl=TRUE) # [1] "Zip Code" "State" "Final Count" 
 在replace参数gsub()的一些细节中, \\1表示“使用匹配第一个子expression式的x的部分”,即x匹配的部分(^|[[:spacde:]]) 。 同样, \\2表示使用匹配第二个子expression式([[:alpha:]])的x部分。  \\U是通过使用perl=TRUE启用的语法,并且意味着使下一个字符成为大写。 因此,对于“邮政编码”, \\1是“邮政编码”, \\2是“代码”, \\U\\2是“代码”,和\\1\\U\\2是“邮政编码”。 
  ?regexp页面有助于理解正则expression式, ?gsub将所有东西放在一起。 
 使用stringi包中的这个函数 
 stri_trans_totitle(c("zip code", "state", "final count")) ## [1] "Zip Code" "State" "Final Count" stri_trans_totitle("i like pizza very much") ## [1] "I Like Pizza Very Much" 
标题案例还有一个内置的base-R解决scheme :
 tools::toTitleCase("demonstrating the title case") ## [1] "Demonstrating the Title Case" 
要么
 library(tools) toTitleCase("demonstrating the title case") ## [1] "Demonstrating the Title Case" 
尝试:
 require(Hmisc) sapply(name, function(x) { paste(sapply(strsplit(x, ' '), capitalize), collapse=' ') }) 
 从?toupper的帮助页面: 
 .simpleCap <- function(x) { s <- strsplit(x, " ")[[1]] paste(toupper(substring(s, 1,1)), substring(s, 2), sep="", collapse=" ") } > sapply(name, .simpleCap) zip code state final count "Zip Code" "State" "Final Count" 
替代scheme:
 library(stringr) a = c("capitalise this", "and this") a [1] "capitalise this" "and this" str_to_title(a) [1] "Capitalise This" "And This" 
 包BBmisc现在包含函数capitalizeStrings 。 
 library("BBmisc") capitalizeStrings(c("the taIl", "wags The dOg", "That Looks fuNny!") , all.words = TRUE, lower.back = TRUE) [1] "The Tail" "Wags The Dog" "That Looks Funny!" 
子string和正则expression式的替代方法:
 substring(name, 1) <- toupper(substring(name, 1, 1)) pos <- regexpr(" ", name, perl=TRUE) + 1 substring(name, pos) <- toupper(substring(name, pos, pos)) 
 > require(stringr) Loading required package: stringr > str_to_title('the state of the union') [1] "The State Of The Union" 
 轻微更改为simpleCap和.simpleCap答案: 
 simpleCap <- function(x) { s <- strsplit(x, " ") # Remove [[1]] sapply(s, function(x) {paste(toupper(substring(x, 1,1)), substring(x, 2), sep="", collapse=" ")}) # Add sapply into function } 
 我用map_chr而不是sapply如果你不介意在脚本开始加载tidyverse 。 
在stream水线和不同variables中使用它时,我发现这个小小的变化更具可读性和优雅性。
你也可以使用snakecase包(内部包装也是build议的stringi / stringr解决scheme):
 > install.packages("snakecase") > library(snakecase) > name <- c("zip code", "state", "final count") > to_any_case(name, case = "big_camel", postprocess = " ") [1] "Zip Code" "State" "Final Count"