Hakki
Hakki

Reputation: 1472

R cumulative bind.rows

I would like to find away to bind.rows cumulatively. Here is small example, what I would like to achieve. I will demo with gapminder dataset from dslabs package.

library(tidyverse)
library(dslabs)

gapminder %>%
  group_by(year) %>% 
  nest() %>% 
  head(5)

A tibble: 5 x 2
   year data              
  <int> <list>            
1  1960 <tibble [185 x 8]>
2  1961 <tibble [185 x 8]>
3  1962 <tibble [185 x 8]>
4  1963 <tibble [185 x 8]>
5  1964 <tibble [185 x 8]>

I would like to create column, which would bind earlier observations from data column together. So for example row 1 only would have year 1960 data, row 2 would have 1960 + 1961 data, row 3 1960 + 1961 + 1963 data... End result should look something like this.

# A tibble: 5 x 3
   year data               cumulative_data   
  <int> <list>             <list>            
1  1960 <tibble [185 x 8]> <tibble [185 x 8]>
2  1961 <tibble [185 x 8]> <tibble [370 x 8]>
3  1962 <tibble [185 x 8]> <tibble [555 x 8]>
4  1963 <tibble [185 x 8]> <tibble [740 x 8]>
5  1964 <tibble [185 x 8]> <tibble [925 x 8]>

Upvotes: 3

Views: 163

Answers (2)

akrun
akrun

Reputation: 887501

In tidyverse, we can also make use of accumulate from purrr

library(tidyverse)
library(dslabs)
gapminder %>%
   group_by(year) %>% 
   nest() %>% 
   head(5) %>%
   mutate(cumulative_data = accumulate(data, bind_rows)) 
# A tibble: 5 x 3
#   year data               cumulative_data   
#  <int> <list>             <list>            
#1  1960 <tibble [185 × 8]> <tibble [185 × 8]>
#2  1961 <tibble [185 × 8]> <tibble [370 × 8]>
#3  1962 <tibble [185 × 8]> <tibble [555 × 8]>
#4  1963 <tibble [185 × 8]> <tibble [740 × 8]>
#5  1964 <tibble [185 × 8]> <tibble [925 × 8]>

Upvotes: 2

Julius Vainora
Julius Vainora

Reputation: 48221

Reduce with the accumulate = TRUE option allows to achieve that:

gapminder %>%
  group_by(year) %>% 
  nest() %>% 
  head(5) %>%
  mutate(cumulative_data = Reduce(rbind, data, accumulate = TRUE))
# A tibble: 5 x 3
#    year data               cumulative_data   
#   <int> <list>             <list>            
# 1  1960 <tibble [185 × 8]> <tibble [185 × 8]>
# 2  1961 <tibble [185 × 8]> <tibble [370 × 8]>
# 3  1962 <tibble [185 × 8]> <tibble [555 × 8]>
# 4  1963 <tibble [185 × 8]> <tibble [740 × 8]>
# 5  1964 <tibble [185 × 8]> <tibble [925 × 8]>

Upvotes: 3

Related Questions