Injae Jeon
Injae Jeon

Reputation: 83

How can I add rows which has consecutive year values based on the range in R?

I would like transform df to df1

How can I add rows which has consecutive year values based on the range from the start year to the end year?

In short, how can I mutate variable "export relation"?

df <- data.frame(home = c("a", "b", "c"), host = c("d", "e", "f"), export_start = c(2015, 2016, 2016), export_end = c(2015, 2018, 2020), average_ratio = c(0.2, 0.3, 0.5), gap = c(0, 2, 4))
> df
  home host export_start export_end average_ratio gap
1    a    d         2015       2015           0.2   0
2    b    e         2016       2018           0.3   2
3    c    f         2016       2020           0.5   4
> df1
  home host export_relation average_ratio
1    a    d            2015           0.2
2    b    e            2016           0.3
3    b    e            2017           0.3
4    b    e            2018           0.3
5    c    f            2016           0.5
6    c    f            2017           0.5
7    c    f            2018           0.5
8    c    f            2019           0.5
9    c    f            2020           0.5

Upvotes: 1

Views: 84

Answers (1)

Limey
Limey

Reputation: 12471

Here's a tidyverse solution:

library(tidyverse)

df %>% 
  rowwise() %>% 
  group_map(
    function(.x, .y) {
      .x %>% 
        expand(nesting(home, host), export_relation=export_start:export_end)
    }
  ) %>% 
  bind_rows()
# A tibble: 9 × 3
  home  host  export_relation
  <chr> <chr>           <int>
1 a     d                2015
2 b     e                2016
3 b     e                2017
4 b     e                2018
5 c     f                2016
6 c     f                2017
7 c     f                2018
8 c     f                2019
9 c     f                2020

group_map applies the function defined by its argument to each group of the grouped tibble in turn. The arguments of the function should be .x: the data in the current group and .y: a single row tibble containing the columns (and their values) that define the current group. .y is not needed in this case.

group_map returns a list: here, a list of tibbles. bind_rows combines the list into a single tibble.

Upvotes: 3

Related Questions