如何将所有值重新编码为小写但忽略 TRUE 和 FALSE?
How to recode all values to lowercase but ignore TRUE and FALSE?
我有数据要转换为小写并使用 snakecase::to_any_case()
用下划线分隔。但是,我希望忽略包含 TRUE
/FALSE
的单元格并将它们保留为大写。
数据
制作一个可重现的例子
library(dplyr)
df <-
tibble(animals = c("Peregrine falcon", "Golden eagle", TRUE, "White-throated needletail swift"),
cars = c("Hennessey Venom GT", FALSE, "Bugatti Chiron Super Sport 300+", "Hennessey Venom F5"),
airplanes = c("North American X-15", "Lockheed SR-71 Blackbird", FALSE, "Lockheed YF-12")) %>%
mutate_all(as.character)
## animals cars airplanes
## <chr> <chr> <chr>
## 1 Peregrine falcon Hennessey Venom GT North American X-15
## 2 Golden eagle FALSE Lockheed SR-71 Blackbird
## 3 TRUE Bugatti Chiron Super Sport 300+ FALSE
## 4 White-throated needletail swift Hennessey Venom F5 Lockheed YF-12
重新编码数据
library(snakecase)
df %>%
mutate(across(everything(), to_any_case)) # dplyr 1.0.2
## animals cars airplanes
## <chr> <chr> <chr>
## 1 peregrine_falcon hennessey_venom_gt north_american_x_15
## 2 golden_eagle false lockheed_sr_71_blackbird
## 3 true bugatti_chiron_super_sport_300 false
## 4 white_throated_needletail_swift hennessey_venom_f_5 lockheed_yf_12
但我正在努力实现这一目标
animals cars airplanes
<chr> <chr> <chr>
1 peregrine_falcon hennessey_venom_gt north_american_x_15
2 golden_eagle FALSE lockheed_sr_71_blackbird
3 TRUE bugatti_chiron_super_sport_300 FALSE
4 white_throated_needletail_swift hennessey_venom_f_5 lockheed_yf_12
如何在忽略 TRUE
和 FALSE
值的同时 运行 mutate(across(everything(), to_any_case))
?
我不是 dplyr
语法方面的专家,但以下内容应该有效:
library(snakecase)
to_any_no_tf <- function(x) {
ifelse(!grepl("^(?:TRUE|FALSE)$", x), to_any_case(x), x)
}
df %>% mutate(across(everything(), to_any_no_tf))
这里的逻辑是用grepl
判断输入值是字符串TRUE
还是FALSE
。如果是这样,那么我们不会对它们应用任何案例逻辑,否则我们会像您已经在做的那样调用 to_any_case()
。
我有数据要转换为小写并使用 snakecase::to_any_case()
用下划线分隔。但是,我希望忽略包含 TRUE
/FALSE
的单元格并将它们保留为大写。
数据
制作一个可重现的例子
library(dplyr)
df <-
tibble(animals = c("Peregrine falcon", "Golden eagle", TRUE, "White-throated needletail swift"),
cars = c("Hennessey Venom GT", FALSE, "Bugatti Chiron Super Sport 300+", "Hennessey Venom F5"),
airplanes = c("North American X-15", "Lockheed SR-71 Blackbird", FALSE, "Lockheed YF-12")) %>%
mutate_all(as.character)
## animals cars airplanes
## <chr> <chr> <chr>
## 1 Peregrine falcon Hennessey Venom GT North American X-15
## 2 Golden eagle FALSE Lockheed SR-71 Blackbird
## 3 TRUE Bugatti Chiron Super Sport 300+ FALSE
## 4 White-throated needletail swift Hennessey Venom F5 Lockheed YF-12
重新编码数据
library(snakecase)
df %>%
mutate(across(everything(), to_any_case)) # dplyr 1.0.2
## animals cars airplanes
## <chr> <chr> <chr>
## 1 peregrine_falcon hennessey_venom_gt north_american_x_15
## 2 golden_eagle false lockheed_sr_71_blackbird
## 3 true bugatti_chiron_super_sport_300 false
## 4 white_throated_needletail_swift hennessey_venom_f_5 lockheed_yf_12
但我正在努力实现这一目标
animals cars airplanes
<chr> <chr> <chr>
1 peregrine_falcon hennessey_venom_gt north_american_x_15
2 golden_eagle FALSE lockheed_sr_71_blackbird
3 TRUE bugatti_chiron_super_sport_300 FALSE
4 white_throated_needletail_swift hennessey_venom_f_5 lockheed_yf_12
如何在忽略 TRUE
和 FALSE
值的同时 运行 mutate(across(everything(), to_any_case))
?
我不是 dplyr
语法方面的专家,但以下内容应该有效:
library(snakecase)
to_any_no_tf <- function(x) {
ifelse(!grepl("^(?:TRUE|FALSE)$", x), to_any_case(x), x)
}
df %>% mutate(across(everything(), to_any_no_tf))
这里的逻辑是用grepl
判断输入值是字符串TRUE
还是FALSE
。如果是这样,那么我们不会对它们应用任何案例逻辑,否则我们会像您已经在做的那样调用 to_any_case()
。