dplyr::across 中的函数参数用于在多列上进行转换

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

我想使用

dplyr::across
将一些 [0,1] 变量重新编码为级别为 ["no","yes"] 的因子。我成功了,但我想了解如何在两个语法选项中定义函数参数。
across

我在
library(dplyr) library(forcats) # toy dataset df = data.frame( var_a = c(0,1,0,1,0,1,0,1,0,1), var_b = c(0,1,0,1,0,1,0,1,0,1), number_a = 1:10, number_b = 21:30) # selection of columns some_cols = c("var_a", "var_b" ) # ---- 1) class trarnsfomation # this works df2_a <- df %>% mutate(across(.cols = some_cols, .fns = as.factor, .names = "{.col}_f")) # this works too df2_b <- df %>% mutate(across(some_cols, ~ as.factor(.x), .names = "{.col}_f")) # ---- 2) Change factor levels # this DOES NOT work !!! df3_a <- df2_a %>% mutate(across(.cols = ends_with("_f"), .fns = fct_recode, c(yes = "1", no = "0" ))) # this works df3_b <- df2_b %>% mutate(across(ends_with("_f"), ~ fct_recode(.x , yes = "1", no = "0" )))

做错了什么?

    

dplyr across
1个回答
0
投票
df3_a

的每个元素都必须是命名字符串而不是命名向量,即您正在做的是

fct_recode

本来应该如此

~ fct_recode(.x, c(yes = "1", no = "0" ))

因此,要让你的代码正常工作

~ fct_recode(.x, yes = "1", no = "0" )

但是,正如警告告诉我们的那样,
library(dplyr, warn=FALSE) library(forcats) df %>% mutate( across( all_of(some_cols), .fns = as.factor, .names = "{.col}_f" ), across( ends_with("_f"), .fns = fct_recode, yes = "1", no = "0" ) ) #> Warning: There was 1 warning in `mutate()`. #> ℹ In argument: `across(ends_with("_f"), .fns = fct_recode, yes = "1", no = #> "0")`. #> Caused by warning: #> ! The `...` argument of `across()` is deprecated as of dplyr 1.1.0. #> Supply arguments directly to `.fns` through an anonymous function instead. #> #> # Previously #> across(a:b, mean, na.rm = TRUE) #> #> # Now #> across(a:b, \(x) mean(x, na.rm = TRUE)) #> var_a var_b number_a number_b var_a_f var_b_f #> 1 0 0 1 21 no no #> 2 1 1 2 22 yes yes #> 3 0 0 3 23 no no #> 4 1 1 4 24 yes yes #> 5 0 0 5 25 no no #> 6 1 1 6 26 yes yes #> 7 0 0 7 27 no no #> 8 1 1 8 28 yes yes #> 9 0 0 9 29 no no #> 10 1 1 10 30 yes yes

...
参数在
across
中已被弃用。相反,现在应该使用匿名函数,就像您在
dplyr 1.1.0
中所做的那样。
    

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