本文介绍了R multidplyr for summarise_at可以解决吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用multidplyr,并且summarise_at尚未使用任何东西。我有数百个(即使不是数千个),因此summarise_at是必需的,但不幸的是,在multidplyr中不可用。

I want to use multidplyr, and it has yet to have anything for summarise_at. i have hundreds if not thousands, so the summarise_at is necessary, but unfortunately, not available in multidplyr.

寻找替代方案来解决它。

looking for an alternative to work around it.

library('tidyverse')
df <- tibble(ID = c('a','a','b','c','c','e','e','f','g','g'),
              var1 = floor(runif(10, min=0, max=100)),
              var2 = floor(runif(10, min=0, max=100)),
              var3 = floor(runif(10, min=0, max=100)),
              var4 = floor(runif(10, min=0, max=100))
              )

library('multidplyr')
cluster <- new_cluster(5)

#works
df %>% 
  group_by(ID) %>% 
  #partition(cluster) %>% 
  summarise_at(.vars = vars(starts_with('var')),sum) 
  #collect()

#works
df %>% 
  group_by(ID) %>% 
  partition(cluster) %>% 
  summarise(var1 = sum(var1),
            var2 = sum(var2),
            var3 = sum(var3)) %>% 
  collect()

#doesnt works
df %>% 
  group_by(ID) %>% 
  partition(cluster) %>%
  summarise_at(.vars = vars(starts_with('var')),sum)  %>% 
  collect()

我什至尝试过

#Define character string vector to replace command line
sum_var <- select(df,starts_with('var')) %>% names()
sum_var_str <- paste0(sum_var," = sum(",sum_var,")")
sum_var_str <- str_c(sum_var_str, collapse = ", ")
> sum_var
[1] "var1" "var2" "var3" "var4"
> sum_var_str
[1] "var1 = sum(var1), var2 = sum(var2), var3 = sum(var3), var4 = sum(var4)"

#works
df %>% 
  group_by(ID) %>% 
  { eval(parse(text = sprintf("summarise(., %s, .groups = 'drop')", sum_var_str))) }

#doesn't works
df %>% 
  group_by(ID) %>% 
  partition(cluster) %>%
  { eval(parse(text = sprintf("summarise(., %s, .groups = 'drop')", sum_var_str))) } %>%
  collect()


推荐答案

找到了解决方案

library('dplyr')
library('multidplyr')
library('parallel')
cluster <- new_cluster(detectCores())

df <- tibble(ID = c('a','a','b','c','c','e','e','f','g','g'),
             var1 = floor(runif(10, min=0, max=100)),
             var2 = floor(runif(10, min=0, max=100)),
             var3 = floor(runif(10, min=0, max=100)),
             var4 = floor(runif(10, min=0, max=100))
)

sum_var <- select(df,starts_with('var')) %>% names()

#assign vector to cluster
cluster_assign(cluster, sum_var = sum_var)
cluster_library(cluster, 'dplyr')

df %>% 
  group_by(ID) %>% 
  partition(cluster) %>% 
  summarise(across(all_of(sum_var), sum)) %>% 
  collect()

# A tibble: 6 x 5
  ID     var1  var2  var3  var4
  <chr> <dbl> <dbl> <dbl> <dbl>
1 a        57    72    85   118
2 b        46    50    80    33
3 c        82   156    96   154
4 e       122   107    93   120
5 f        33     7    49    36
6 g        99    79    83    56

这篇关于R multidplyr for summarise_at可以解决吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 01:42