在dplyr 1.0中从mutate_all移动到cross()
随着新版本的dplyr的发布,我正在重构相当多的代码并删除现在已经失效或不推荐使用的函数。我有一个函数,如下所示:在dplyr 1.0中从mutate_all移动到cross(),r,dplyr,R,Dplyr,随着新版本的dplyr的发布,我正在重构相当多的代码并删除现在已经失效或不推荐使用的函数。我有一个函数,如下所示: processingAggregatedLoad <- function (df) { defined <- ls() passed <- names(as.list(match.call())[-1]) if (any(!defined %in% passed)) { stop(paste("Missing values fo
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)
}
我做了多次试验,例如:
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\u each
取代mutate\u all
现在被跨mutate\u all
将默认值cross
设置为.cols
,这意味着如果未明确提及,默认情况下它的行为方式为everything()
mutate\u 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)
}
库(dplyr)
处理聚合负载
mutate\u each
早已被弃用,并被mutate\u all
取代
mutate\u all
现在被跨
cross
将默认值.cols
设置为everything()
,这意味着如果未明确提及,默认情况下它的行为方式为mutate\u 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)
}
库(dplyr)
processingAggregatedLoad@10Rep我无法在这里添加任何预期的输出,因为OP没有共享任何数据。是的,我想是这样的。尽量不要回答这类问题。亲爱的同事们,很抱歉我没有附上任何数据,因为我上周没有可能收到任何邮件。@10Rep我无法在这里添加任何预期的输出,因为OP没有共享任何数据。是的,我想是这样的。尽量不要回答这些类型的问题。亲爱的同事们,很抱歉没有附上任何数据,因为我上周不可能再回答了。亲爱的Ronah,你的回答非常有效。非常感谢您的帮助,并为提供一个可复制的示例表示歉意。BR/EDear Ronah,你的答案非常有效。非常感谢您的帮助,并为提供一个可复制的示例表示歉意。BR/E