如何使用 case_when() 改变除选定值向量之外的所有值

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

我正在清理企业名称列表,并且正在努力有选择地将案例转换为标题案例。我可以使用

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 
r stringr mutate
1个回答
0
投票

您可以使用

过滤掉矢量示例
case_when( !(`Company Name`%in% c("ABC Company","John Doe Company LLC","XYZ")) ~ str_to_title(`Company Name`), .default = `Company Name`)

或者您可以使用仅大写以小写字母开头的字符串的函数:

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

# Function to capitalize words selectively
capitalize_words <- function(input_string) {
  str_replace_all(input_string, "\\b[a-z][a-z]*\\b", function(word) {
    str_to_title(word)
  })
}

test<-test%>%
  mutate(`Company Name`= case_when( !(`Company Name`%in% c("ABC Company","John Doe Company LLC","XYZ")) ~ str_to_title(`Company Name`), .default = `Company Name`),
         `Capitalized Company Names` = capitalize_words(`Company Name`))

最终得到这个结果:

output

© www.soinside.com 2019 - 2024. All rights reserved.