首页 > 解决方案 > 在 dplyr 1.0 中从 mutate_all 移动到 cross()

问题描述

随着 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(.)) # we get isolated load
  df_isolated_load_qlty <- df %>% select(matches("qlty_good_ind")) # we get isolated quality
  df_isolated_load_qlty <- df_isolated_load_qlty %>% mutate_all(~ factor(.), colnames(df_isolated_load_qlty)) %>%
  mutate_each(funs(as.numeric(.)), colnames(df_isolated_load_qlty)) # we convert the qlty to factors and then to numeric
  df_isolated_load_qlty[df_isolated_load_qlty[]==1] <- 1  # 1 is bad
  df_isolated_load_qlty[df_isolated_load_qlty[]==2] <- 0 # 0 is good we mask to calculate the global index quality
  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)
}

基本上该功能如下:

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

2.该函数选择旋转数据帧的质量指标(字符)。

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

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

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

问题是我收到以下消息:

1: `funs()` is deprecated as of dplyr 0.8.0.
Please use a list of either functions or lambdas: 

  # Simple named list: 
  list(mean = mean, median = median)

  # Auto named with `tibble::lst()`: 
  tibble::lst(mean, median)

  # Using lambdas
  list(~ mean(., trim = .2), ~ median(., na.rm = TRUE))
This warning is displayed once every 8 hours.
Call `lifecycle::last_warnings()` to see where this warning was generated. 
2: `mutate_each_()` is deprecated as of dplyr 0.7.0.
Please use `across()` instead.

我做了多次试验,例如:

 df_isolated_load_qlty %>% mutate(across(.fns = ~ as.factor(), .names = colnames(df_isolated_load_qlty)))
Error: Problem with `mutate()` input `..1`.
x All unnamed arguments must be length 1
ℹ Input `..1` is `across(.fns = ~as.factor(), .names = colnames(df_isolated_load_qlty))`.

但是我对新的 dplyr 语法仍然有些困惑。有人可以指导我一些正确的方法吗?

标签: rdplyr

解决方案


  • mutate_each早已被弃用并被替换为mutate_all.
  • mutate_all现在被替换为across
  • across具有默认值.colseverything()这意味着如果未明确提及,它的行为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)
  }

推荐阅读