r - 在 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 语法仍然有些困惑。有人可以指导我一些正确的方法吗?
解决方案
mutate_each
早已被弃用并被替换为mutate_all
.mutate_all
现在被替换为across
across
具有默认值.cols
,everything()
这意味着如果未明确提及,它的行为mutate_all
默认为(如此处)。- 你可以在同一个调用中应用多个函数
mutate
,所以这里factor
和as.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)
}
推荐阅读
- angular - 使用 ngx-extended-pdf-viewer 在 Angular 组件中显示 PDF 文件
- javascript - 如何使用 AWS Lambda 中的 websocket 将响应发送回客户端
- java - 执行并行 Stream Java 8 时出现异常
- objective-c - 如何在 Objective C 框架中通过 APNS 接收来自 Firebase 云消息传递的通知?
- c# - 从表达式或动态 Linq 中获取类名
- python - 在 Python Pandas 中将有序字典转换为新列
- javascript - 获取二分查找中的迭代次数
- azure - Azure AD 域服务
- mysql - 为什么mysql会使用utf8mb4_unicode_ci排序规则生成乱码?
- php - Swiftmailler 两次发送相同的电子邮件