Kon*_*rad 9 string r capitalization toupper tolower
同事,
我正在查看类似于以下摘录的数据框:
Month Provider Items
January CofCom 25
july CofCom 331
march vobix 12
May vobix 0
Run Code Online (Sandbox Code Playgroud)
我想将每个单词的首字母大写,并降低每个单词的剩余字母.这将导致数据框类似于下面的数据框:
Month Provider Items
January Cofcom 25
July Cofcom 331
March Vobix 12
May Vobix 0
Run Code Online (Sandbox Code Playgroud)
总之,我正在寻找R等同于MS Excel中可用的ROPER功能.
Mat*_*rde 26
使用正则表达式:
x <- c('woRd Word', 'Word', 'word words')
gsub("(?<=\\b)([a-z])", "\\U\\1", tolower(x), perl=TRUE)
# [1] "Word Word" "Word" "Word Words"
Run Code Online (Sandbox Code Playgroud)
(?<=\\b)([a-z])说找一个小写字母前面有一个单词边界(例如,一个空格或一行的开头).(?<=...)被称为"后视"断言.\\U\\1用它的大写版本替换该字符.\\1是()模式中包围的第一个组的后向引用.有关?regex详细信息,请参阅
如果您只想将第一个单词的第一个字母大写,请改用该模式"^([a-z]).
ant*_*nio 10
问题是关于等效的Excel PROPER和(前)接受的答案是基于:
proper=function(x) paste0(toupper(substr(x, 1, 1)), tolower(substring(x, 2)))
Run Code Online (Sandbox Code Playgroud)
值得注意的是:
proper("hello world")
## [1] "Hello world"
Run Code Online (Sandbox Code Playgroud)
PROPER相反,Excel 会给出"Hello World".使用Excel进行1:1映射时请参阅@Matthew Plourde.
如果你真正需要的是只将字符串的第一个字符设置为大写,你可能还会考虑更短和更快的版本:
proper=function(s) sub("(.)", ("\\U\\1"), tolower(s), pe=TRUE)
Run Code Online (Sandbox Code Playgroud)
另一种方法使用stringi包.stri_trans_general函数似乎小写除了首字母以外的所有字母.
require(stringi)
x <- c('woRd Word', 'Word', 'word words')
stri_trans_general(x, id = "Title")
[1] "Word Word" "Word" "Word Words"
Run Code Online (Sandbox Code Playgroud)
我不认为有一个,但你可以轻松自己写
(dat <- data.frame(x = c('hello', 'frIENds'),
y = c('rawr','rulZ'),
z = c(16, 18)))
# x y z
# 1 hello rawr 16
# 2 frIENds rulZ 18
proper <- function(x)
paste0(toupper(substr(x, 1, 1)), tolower(substring(x, 2)))
(dat <- data.frame(lapply(dat, function(x)
if (is.numeric(x)) x else proper(x)),
stringsAsFactors = FALSE))
# x y z
# 1 Hello Rawr 16
# 2 Friends Rulz 18
str(dat)
# 'data.frame': 2 obs. of 3 variables:
# $ x: chr "Hello" "Friends"
# $ y: chr "Rawr" "Rulz"
# $ z: num 16 18
Run Code Online (Sandbox Code Playgroud)