Geet
Geet

Reputation: 2575

Fill sequence conditional on other variables in R

Here is my data:

df <- tibble::tribble(
  ~Group, ~Year, ~given, ~required,
     "A", 2017L,     3L,        1L,
     "A", 2017L,     4L,        2L,
     "A", 2017L,     8L,        6L,
     "A", 2018L,     1L,        7L,
     "A", 2018L,     4L,       10L,
     "B", 2018L,     8L,        1L,
     "B", 2019L,     3L,        4L,
     "B", 2019L,     4L,        5L)

I want to calculate "required" such that, for any "Group":

  1. The first entry of the 'required' gets the value of 1.
  2. The delta between the 'required' and 'given' variables has to be the same.
  3. For any Year, the minimum values for "given" variable could be 1 and the maximum is 8.

How should I calculate 'required' variable using the 'Group', 'Year', and 'given' variables?

Upvotes: 0

Views: 57

Answers (1)

www
www

Reputation: 39174

library(tidyverse)

df2 <- df %>%
  group_by(Group) %>%
  mutate(required = c(1, diff(given)),
         required = ifelse(required < 0, max(given) - abs(required), required),
         required = cumsum(required)) %>%
  ungroup()
df2
# # A tibble: 8 x 4
#   Group  Year given required
#   <chr> <int> <int>    <dbl>
# 1 A      2017     3        1
# 2 A      2017     4        2
# 3 A      2017     8        6
# 4 A      2018     1        7
# 5 A      2018     4       10
# 6 B      2018     8        1
# 7 B      2019     3        4
# 8 B      2019     4        5

Upvotes: 1

Related Questions