191

假设我有一个两个单词的字符串,我想将它们都大写。

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")
4

14 回答 14

188

标题案例也有一个内置的base-R 解决方案:

tools::toTitleCase("demonstrating the title case")
## [1] "Demonstrating the Title Case"

或者

library(tools)
toTitleCase("demonstrating the title case")
## [1] "Demonstrating the Title Case"
于 2016-07-22T13:26:19.940 回答
181

执行大写的基本 R 函数是toupper(x). 从帮助文件中?toupper有这个功能可以满足您的需求:

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" 

编辑这适用于任何字符串,无论字数如何:

simpleCap("I like pizza a lot")
[1] "I Like Pizza A Lot"
于 2011-06-15T21:58:00.680 回答
102

^匹配一个正则表达式,该正则表达式从空格开头或之后开始[[:space:]],后跟一个字母字符[[:alpha:]]。全局(gsub 中的 g)用匹配的开头或空格以及匹配的字母字符的大写版本替换所有此类出现\\1\\U\\2。这必须通过 perl 风格的正则表达式匹配来完成。

gsub("(^|[[:space:]])([[:alpha:]])", "\\1\\U\\2", name, perl=TRUE)
# [1] "Zip Code"    "State"       "Final Count"

更详细地说,替换参数是gsub()\\1使用x匹配第一个子表达式的部分”,即x匹配的部分(^|[[:spacde:]])。同样,\\2表示使用x匹配第二个子表达式的部分([[:alpha:]])\\Uis 使用 , 启用语法,perl=TRUE表示将下一个字符变为大写。所以对于“Zip code”,\\1是“Zip”,\\2是“code”,\\U\\2是“Code”,\\1\\U\\2是“Zip Code”。

?regexp页面有助于理解正则表达式,?gsub将事物放在一起。

于 2011-06-15T22:49:34.660 回答
84

使用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"
于 2014-03-14T09:38:43.517 回答
53

选择:

library(stringr)
a = c("capitalise this", "and this")
a
[1] "capitalise this" "and this"       
str_to_title(a)
[1] "Capitalise This" "And This"   
于 2016-05-28T16:27:45.120 回答
21

尝试:

require(Hmisc)
sapply(name, function(x) {
  paste(sapply(strsplit(x, ' '), capitalize), collapse=' ')
})
于 2011-06-15T21:57:59.393 回答
16

从帮助页面?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"
于 2011-06-15T22:13:31.383 回答
9

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!"
于 2015-02-09T14:46:08.430 回答
6

子字符串和正则表达式的替代方法:

substring(name, 1) <- toupper(substring(name, 1, 1))
pos <- regexpr(" ", name, perl=TRUE) + 1
substring(name, pos) <- toupper(substring(name, pos, pos))
于 2015-01-14T01:54:11.807 回答
5

您还可以使用蛇盒包:

install.packages("snakecase")
library(snakecase)

name <- c("zip code", "state", "final count")
to_title_case(name)
#> [1] "Zip Code"    "State"       "Final Count"

# or 
to_upper_camel_case(name, sep_out = " ")
#> [1] "Zip Code"    "State"       "Final Count"

https://github.com/Tazinho/snakecase

于 2017-03-25T23:20:48.797 回答
2

这为所有主要单词提供大写字母

library(lettercase)
xString = str_title_case(xString)
于 2018-02-06T19:07:39.273 回答
2

另一个版本StrCap使用DescTools

Text = c("This is my phrase in r", "No, this is not my phrase in r")

DescTools::StrCap(Text) # Default only first character capitalized
[1] "This is my phrase in r"         "No, this is not my phrase in r"

DescTools::StrCap(Text, method = "word") # Capitalize each word
[1] "This Is My Phrase In R"        "No This Is Not My Phrase In R"

> DescTools::StrCap(Text, method = "title") # Capitalize as in titles
[1] "This Is My Phrase in R"         "No, This Is Not My Phrase in R"
于 2021-04-17T22:58:38.470 回答
1

✓ 一条线路
✓ 一项现有功能;没有新包
✓适用于列表/所有单词
✓首字母大写并降低单词的其余部分:

name <- c("zip CODE", "statE", "final couNt")
gsub("([\\w])([\\w]+)", "\\U\\1\\L\\2", name, perl = TRUE)
[1] "Zip Code"    "State"       "Final Count"

如果你打算经常使用它,我想你可以用它做一个包装函数:

capFirst <- function(x) gsub("([\\w])([\\w]+)", "\\U\\1\\L\\2", x, perl = TRUE)
capFirst(name)
于 2021-05-14T16:59:58.200 回答
0

这是对已接受答案的轻微改进,可避免使用sapply(). 还强制非第一个字符降低。

titleCase <- Vectorize(function(x) {
  
  # Split input vector value
  s <- strsplit(x, " ")[[1]]
  
  # Perform title casing and recombine
  ret <- paste(toupper(substring(s, 1,1)), tolower(substring(s, 2)),
        sep="", collapse=" ")
  
  return(ret)
  
}, USE.NAMES = FALSE)


name <- c("zip CODE", "statE", "final couNt")

titleCase(name)

#> "Zip Code"       "State" "Final Count" 

于 2020-09-20T02:34:32.290 回答