Skip to content

Instantly share code, notes, and snippets.

@MilesMcBain
Created January 9, 2021 10:11
Show Gist options
  • Select an option

  • Save MilesMcBain/e0da1a1a1f1a6cbc3e9b7364bc9b33fe to your computer and use it in GitHub Desktop.

Select an option

Save MilesMcBain/e0da1a1a1f1a6cbc3e9b7364bc9b33fe to your computer and use it in GitHub Desktop.
library(tidyverse)
first_year <- tibble(year = 2010, z = 1, x = 2, y = 3)
all_years <- first_year %>%
bind_rows(tibble(year = 2011:2015))
all_years %>%
summarise(
year = year,
accumulate(
.init = tibble(
x = first(all_years$x),
y = first(all_years$y),
z = first(all_years$z)
),
.x = seq(length(z) - 1), # because we're supplying 1 row in init - tripped me up
.f = function(inputs, x_ignored) {
tibble(
z = 0.5 * inputs$x,
x = 0.5 * z + 0.5 * inputs$y,
y = 0.25 * inputs$x
)
}
) %>%
bind_rows()
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment