在R中工作,我希望Arrow对变量中指定的一组列求和.
library(arrow)
library(dplyr)
example_data = InMemoryDataset$create(data.frame(a1 = c(1,2,3), b2=c(4,5,6), c3=c(7,8,9)))
cols_to_sum = c('a1','b2','c3')
Arrow能够做到这一点:
example_data %>% mutate(computed_sum = a1+b2+c3) %>% compute()
#Succeeds
但是,我希望传递变量,而不是显式地指定列.我通常使用的dplyr语法不适用于Arrow:
example_data %>%
mutate(computed_sum = rowSums(across(all_of(cols_to_sum)))) %>%
compute()
#Error: Expression rowSums(across(all_of(cols_to_sum))) not supported in Arrow
#Call collect() first to pull data into R.
使用parse()和val()重新构建文字输入字符串确实有效,但对于本应是常见操作的操作来说,这似乎是一种繁琐的解决办法:
temp_expression = parse( text=paste(cols_to_sum, collapse = '+') )
example_data %>%
mutate(computed_sum = eval(temp_expression) ) %>%
compute()
#Succeeds
但是,在没有显式临时变量的情况下,上述过程失败:
example_data %>%
mutate(computed_sum = eval( parse( text=paste(cols_to_sum, collapse = '+') ) ) ) %>%
compute()
#Error: Expression eval(parse(text = paste(cols_to_sum, collapse = "+"))) not supported in Arrow
#Call collect() first to pull data into R.
使用Arrow的R接口在变量中列出的列上指定递归计算(例如,SUM)的正确/最佳/预期方式是什么?要实现这一点,我需要构建字符串并对它们求值()吗?
非Arrow解决方案对我不起作用.我正在处理的数据对于内存来说太大了,这些数据以配置单元分区的形式分发,并由Arrow的Open_DataSet()访问.