Search code examples
rdplyrfillexpand

How to expand rows and fill in the numbers between given start and end


I have this data frame:

df <- tibble(x = c(1, 10))

      x
  <dbl>
1     1
2    10

I want this:

      x
   <int>
 1     1
 2     2
 3     3
 4     4
 5     5
 6     6
 7     7
 8     8
 9     9
10    10

Unfortunately I can't remember how I have to approach. I tried expand.grid, uncount, runner::fill_run.

Update: The real world data ist like this with groups and given start and end number. Here are only two groups:

  df <- tibble(group = c("A", "A", "B", "B"), 
               x = c(10,30, 1, 10)) 

  group     x
  <chr> <dbl>
1 A        10
2 A        30
3 B         1
4 B        10

Solution

  • We may need full_seq with either summarise or reframe or tidyr::complete

    library(dplyr)
    df %>% 
       group_by(group) %>%
       reframe(x = full_seq(x, period = 1))
       # or with 
       #tidyr::complete(x = full_seq(x, period = 1))
    

    -output

    # A tibble: 31 × 2
       group     x
       <chr> <dbl>
     1 A        10
     2 A        11
     3 A        12
     4 A        13
     5 A        14
     6 A        15
     7 A        16
     8 A        17
     9 A        18
    10 A        19
    # … with 21 more rows