我正在清理公司名称列表,但很难有选择地将大小写转换为标题大小写。我可以使用mutate(str_to_title(...))
函数将整个字段转换为标题大小写,这对我的大多数值都很有效,但有少数公司的标题是“ABC Company”或“John Doe Company LLC”,当我应用标题大小写时,它们的正确大小写就会混乱(“Abc Company”和“John Doe Company Llc”)。
我以为我可以使用case_when()
特定值的向量来创建一个函数,该函数告诉 R 仅将标题大小写应用于不等于我指定的值向量的值。但是,我要么收到警告“较长的对象长度不是较短的对象长度的倍数”,并且所有值都转换为标题大小写,要么我只是得到字段中的向量值的 NA,并为不在向量中的值更正标题大小写值。我哪里做错了?
# Example Code #
library(tidyverse)
## Reproducible Example ##
test<-structure(list(`Company Name` = c("ABC Company", "John Doe Company LLC",
"rainbow road company", "yellow brick road incorporated", "XYZ",
"Mostly Ghostly Company", "hot Leaf juice tea company")), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -7L))
test<-test%>%
mutate(`Company Name`= case_when(`Company Name`!= c("ABC Company","John Doe Company LLC","XYZ") ~ str_to_title(`Company Name`)))
# Error #
Warning message:
There was 1 warning in `mutate()`.
ℹ In argument: `Company Name = case_when(...)`.
Caused by warning in `` `Company Name` != c("ABC Company", "John Doe Company LLC", "XYZ") ``:
! longer object length is not a multiple of shorter object length