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

Time:01-19

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

CodePudding user response:

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

CodePudding user response:

A simple base R variation:

group <- c(rep("A", 21), rep("B ", 10))
x <- c(10:30, 1:10)
df <- tibble(group, x)
df
# A tibble: 31 × 2
   group     x
   <chr> <int>
 1 A        10
 2 A        11
 3 A        12
 4 A        13
 5 A        14
 6 A        15

And here's an expand.grid solution:

g1 <- expand.grid(group = "A", x = 20:30)
g2 <- expand.grid(group = "B", x = 1:10)
df <- rbind(g1, g2)
df
   group  x
1      A 20
2      A 21
3      A 22
4      A 23
5      A 24
6      A 25
7      A 26

CodePudding user response:

Using base:

stack(sapply(split(df$x, df$group), function(i) seq(i[ 1 ], i[ 2 ])))
  • Related