Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
169 views
in Technique[技术] by (71.8m points)

r - SparkR : How to use a list in summarize

I'm trying to use a list that containing all columns of Spark DataFrame with function last() and put that list in summarize() of grouped DF.

The list is created in this way :

    exprs <- lapply(columns(WORK02), function(x) last(x) %>% alias(x))

And then i trying to make somthing like this :

WORK03 <- WORK02 %>% 
  groupBy(column("AGENZIA")) %>%
  summarize(exprs)

I get the following error :

Error in agg(x, ...) : agg can only support Column or character

If i use per example only one element of that list, it works:

WORK03 <- WORK02 %>% 
  groupBy(column("AGENZIA")) %>%
  summarize(exprs[[1]])

Output :

 AGENZIA SERV
       1    3

But the result that i expect to receive is :

 AGENZIA SERV COLUMN_1 COLUMN_2 COLUMN_3 ..  COLUMN_N
       1    3       V1       V2       V3           VN 

Existing an way to do that ?

P.S. Problem is that i can use a limited package of library , only SparkR...

question from:https://stackoverflow.com/questions/66063308/sparkr-how-to-use-a-list-in-summarize

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The solution I found is the following :

For create list of columns what i am trying to select i used function lapply()

exprs <- lapply(columns(WORK02), function(x) last(x) %>% alias(x)) 

After that, i removed all null values from the list, (this option is in case if someone want to don't select a specific column, otherwise column will be created as NULL , that will give to us an error after).

exprs <- exprs[!sapply(exprs,is.null)]

And finally i performing groupBy() + summarize() with function do.call() in the following mode:

WORK_TEST1 <- WORK02 %>% 
  groupBy(column("AGENZIA"))

WORK03 <- do.call(summarize,c(WORK_TEST1,exprs))      

With this iteration, i receive the result that i'm expecting:

 AGENZIA SERV COLUMN_1 COLUMN_2 COLUMN_3 ..  COLUMN_N
       1    3       V1       V2       V3           VN 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...