在 dplyr 1.0 中从 mutate_all 迁移到 across()

tfk*_*STM 3 r dplyr

在新版本的 dplyr 中,我重构了大量代码并删除了现已退休或弃用的函数。我有一个功能如下:

\n
processingAggregatedLoad <- function (df) {\n  defined <- ls()\n  passed <- names(as.list(match.call())[-1])\n  \n  if (any(!defined %in% passed)) {\n    stop(paste("Missing values for the following arguments:", paste(setdiff(defined, passed), collapse=", ")))\n  }\n  \n  df_isolated_load <- df %>% select(matches("snsr_val")) %>% mutate(global_demand = rowSums(.)) # we get isolated load\n  df_isolated_load_qlty <- df %>% select(matches("qlty_good_ind")) # we get isolated quality\n  df_isolated_load_qlty <- df_isolated_load_qlty %>% mutate_all(~ factor(.), colnames(df_isolated_load_qlty)) %>%\n  mutate_each(funs(as.numeric(.)), colnames(df_isolated_load_qlty)) # we convert the qlty to factors and then to numeric\n  df_isolated_load_qlty[df_isolated_load_qlty[]==1] <- 1  # 1 is bad\n  df_isolated_load_qlty[df_isolated_load_qlty[]==2] <- 0 # 0 is good we mask to calculate the global index quality\n  df_isolated_load_qlty <- df_isolated_load_qlty %>% mutate(global_quality = rowSums(.)) %>% select(global_quality)\n  df <- bind_cols(df, df_isolated_load, df_isolated_load_qlty)\n  return(df)\n}\n
Run Code Online (Sandbox Code Playgroud)\n

基本上该函数的作用如下:

\n

1.该函数选择透视数据帧的所有值并将它们聚合。

\n

2.该函数选择透视数据帧的质量指示器(字符)。

\n

3.我将质量的字符转换为因子,然后转换为数字以获得2个级别(1或2)。

\n

4.我根据级别将每一列的数值替换为 0 或 1。

\n

5.我对个体质量进行求和,因为如果所有值都很好,我将得到 0,否则全局质量很差。

\n

问题是我收到以下消息:

\n
1: `funs()` is deprecated as of dplyr 0.8.0.\nPlease use a list of either functions or lambdas: \n\n  # Simple named list: \n  list(mean = mean, median = median)\n\n  # Auto named with `tibble::lst()`: \n  tibble::lst(mean, median)\n\n  # Using lambdas\n  list(~ mean(., trim = .2), ~ median(., na.rm = TRUE))\nThis warning is displayed once every 8 hours.\nCall `lifecycle::last_warnings()` to see where this warning was generated. \n2: `mutate_each_()` is deprecated as of dplyr 0.7.0.\nPlease use `across()` instead.\n
Run Code Online (Sandbox Code Playgroud)\n

我做了多次试验,例如:

\n
 df_isolated_load_qlty %>% mutate(across(.fns = ~ as.factor(), .names = colnames(df_isolated_load_qlty)))\nError: Problem with `mutate()` input `..1`.\nx All unnamed arguments must be length 1\n\xe2\x84\xb9 Input `..1` is `across(.fns = ~as.factor(), .names = colnames(df_isolated_load_qlty))`.\n
Run Code Online (Sandbox Code Playgroud)\n

但我对新的 dplyr 语法仍然有点困惑。有人能够指导我采取正确的方法吗?

\n

Ron*_*hah 5

  • mutate_each已被长期弃用并被替换为mutate_all.
  • mutate_all现在被替换为across
  • across具有默认.cols值,everything()这意味着如果未明确提及,它的行为将mutate_all与默认情况一样(如此处)。
  • 您可以在同一个调用中应用多个函数mutate,因此这里factoras.numeric可以一起应用。

考虑到所有这些,您可以将现有函数更改为:

library(dplyr)

processingAggregatedLoad <- function (df) {
      defined <- ls()
      passed <- names(as.list(match.call())[-1])

     if (any(!defined %in% passed)) {
            stop(paste("Missing values for the following arguments:", 
             paste(setdiff(defined, passed), collapse=", ")))
      }

     df_isolated_load <- df %>% 
                          select(matches("snsr_val")) %>% 
                          mutate(global_demand = rowSums(.))
    df_isolated_load_qlty <- df %>% select(matches("qlty_good_ind"))
    df_isolated_load_qlty <- df_isolated_load_qlty %>% 
                               mutate(across(.fns = ~as.numeric(factor(.))))
                          
    df_isolated_load_qlty[df_isolated_load_qlty ==1] <- 1  
    df_isolated_load_qlty[df_isolated_load_qlty==2] <- 0
    df_isolated_load_qlty <- df_isolated_load_qlty %>% 
                               mutate(global_quality = rowSums(.)) %>% 
                               select(global_quality)
    df <- bind_cols(df, df_isolated_load, df_isolated_load_qlty)
    return(df)
  }
Run Code Online (Sandbox Code Playgroud)