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
3.7k views
in Technique[技术] by (71.8m points)

r - Split 2 separate data frames, apply functions simultaneously to both then combine

I have 2 data frames:

df1 has a list of people who received vouchers at various weeks for a year. It shows which week each customer received their voucher. df2 has daily transactions for the people in df1.

Each data frame has millions of rows

I would like to: 1- Split df1 by week, resulting in 52 data frames (df1.1, df1.2, df1.3.....df1.52) 2- for each of the 52 data frames I would like to do the following:

df2[df2$customer_ID %in% df1.1$customer_ID, ] %>%
   group_by(week_num) %>%
   summarise(tot_sales = sum(sales)

Each time the loop creates a data frame containing one row, i.e a single week.

So the resulting data frame (df3) will have 52 rows.

So far I have the following:

datalist <- list()

df1_split <- split(df1, df1$week_number)  

for (i in seq_along(df1_split)){
    
    for (j in df2$week_number){

   df2[df2$customer_ID %in% df1_split[[i]]$customer_ID, ] %>%
   mutate(week_received = j) %>%
   group_by(week_received) %>%
   summarise(tot_sales = sum(sales))

   datalist[[i]] <- dat
 
    {
 {

df3 <- bind_rows(datalist)

But this just runs continuously. What am I doing wrong?

Thank you in advance.


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

1 Reply

0 votes
by (71.8m points)

Inside the nested for loop, subset the df1_split[[i]] instead of the original data

datalist <- list()

df1_split <- split(df1, df1$week_number)  

for (i in seq_along(df1_split)){

  for (j in df2$week_number){

      tmp <- df2[df2$customer_ID %in% df1_split[[i]]$customer_ID, ] %>%
        mutate(week_received = j) %>%
         group_by(week_received) %>%
         summarise(tot_sales = sum(sales))

         datalist <- c(datalist, list(dat))

    }
  }

 df3 <- bind_rows(datalist)

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

...