apply reduce to each row, containing a list, of a column in R dataframe

Viewed 78

I've included 20 rows of my dataframe:

structure(list(countyfips = c(1003, 1003, 1003, 1003, 1003, 1005, 
1005, 1005, 1005, 1005, 1007, 1007, 1007, 1007, 1007, 1009, 1009, 
1009, 1009, 1009), engagement = c("-.186", "-.231", "-.0681", 
"-.38", "-.267", "-.0148", ".00322", ".0804", "-.478", "-.83", 
"-.0532", "-.162", "-.0185", "-.883", "-.909", ".0278", "-.537", 
"-.691", "-.972", "-.981")), row.names = c(NA, -20L), class = c("tbl_df", 
"tbl", "data.frame"))

which I've grouped using the following expression:

math_stu_online_engage %>%
  group_by(countyfips) %>% summarise(monthly_engagement = list(engagement))

I would now like to apply the following Reduce function to each list / row of the monthly engagement column:

mutate(acc_perc_change = Reduce(function(x, y) x + x * y, monthly_engagement))

But I'm getting this error:

Error: Problem with `mutate()` input `acc_perc_change`.
x non-numeric argument to binary operator

What am I doing wrong here?

Thanks so much!

2 Answers

Here is a solution in base R:

do.call(rbind, lapply(unique(df$countyfips), function(a) {
  tmp <- subset(df, countyfips == a)
  tmp <- transform(tmp, engagement = as.numeric(engagement))
  tmp$acc_perc_change <- Reduce(function(x, y) {
    x + x * y
  }, tmp$engagement)
  tmp
}))

Here is a simplified tidyverse solution:

library(purrr)

df %>%
  group_by(countyfips) %>%
  summarise(engagement = as.numeric(engagement), 
            acc_perc_change = reduce(engagement, ~ .x + .x * .y))

It is a character column. So, we need to first convert it to numeric. Secondly, the reduce/Reduce output is a length of 5 whereas the number of rows is just 4. So, we may need to wrap it in a list

library(dplyr)
library(purrr)
df1 %>% 
    group_by(countyfips) %>%
    summarise(monthly_engagement = as.numeric(engagement)) %>% 
    mutate(acc_perc_change = 
          reduce(monthly_engagement, ~ .x + .x * .y)) %>%
    ungroup

-ouptut

# A tibble: 20 x 3
   countyfips monthly_engagement acc_perc_change
        <dbl>              <dbl>           <dbl>
 1       1003           -0.186       -0.0606    
 2       1003           -0.231       -0.0606    
 3       1003           -0.0681      -0.0606    
 4       1003           -0.38        -0.0606    
 5       1003           -0.267       -0.0606    
 6       1005           -0.0148      -0.00142   
 7       1005            0.00322     -0.00142   
 8       1005            0.0804      -0.00142   
 9       1005           -0.478       -0.00142   
10       1005           -0.83        -0.00142   
11       1007           -0.0532      -0.000466  
12       1007           -0.162       -0.000466  
13       1007           -0.0185      -0.000466  
14       1007           -0.883       -0.000466  
15       1007           -0.909       -0.000466  
16       1009            0.0278       0.00000212
17       1009           -0.537        0.00000212
18       1009           -0.691        0.00000212
19       1009           -0.972        0.00000212
20       1009           -0.981        0.00000212
Related