Home > front end >  Repeat a df based on one of it's numeric sequence fields
Repeat a df based on one of it's numeric sequence fields

Time:09-24

A data frame:

mydf <- data.frame(
  x = c(1:7, 1:7, 1:7),
  y = c(rep('a', 7), rep('b', 7), rep('c', 7)),
  z = c(rep('d', 7), rep('e', 7), rep('f', 7))
)

mydf
   x y z
1  1 a d
2  2 a d
3  3 a d
4  4 a d
5  5 a d
6  6 a d
7  7 a d
8  1 b e
9  2 b e
10 3 b e
11 4 b e
12 5 b e
13 6 b e
14 7 b e
15 1 c f
16 2 c f
17 3 c f
18 4 c f
19 5 c f
20 6 c f
21 7 c f

It's actually grouped:

my_grped_df <- mydf %>% group_by(y, z) %>% group_split

Looks like this:

my_grped_df
<list_of<
  tbl_df<
    x: integer
    y: character
    z: character
  >
>[3]>
[[1]]
# A tibble: 7 × 3
      x y     z    
  <int> <chr> <chr>
1     1 a     d    
2     2 a     d    
3     3 a     d    
4     4 a     d    
5     5 a     d    
6     6 a     d    
7     7 a     d    

[[2]]
# A tibble: 7 × 3
      x y     z    
  <int> <chr> <chr>
1     1 b     e    
2     2 b     e    
3     3 b     e    
4     4 b     e    
5     5 b     e    
6     6 b     e    
7     7 b     e    

[[3]]
# A tibble: 7 × 3
      x y     z    
  <int> <chr> <chr>
1     1 c     f    
2     2 c     f    
3     3 c     f    
4     4 c     f    
5     5 c     f    
6     6 c     f    
7     7 c     f  

I would like to extend each group which currently goes from 1:7 to go all the way to 180. The values of y and z will be the same, but the groups should 'stack' where the value for x increments by 1 all the way to 180.

How can I achieve this?

CodePudding user response:

We may do this in multiple ways i.e. grouped by 'y', 'z', expand with summarise

library(dplyr)
mydf %>%
    group_by(y, z) %>%
    summarise(x = 1:180, .groups = 'drop')

-output

# A tibble: 540 x 3
   y     z         x
   <chr> <chr> <int>
 1 a     d         1
 2 a     d         2
 3 a     d         3
 4 a     d         4
 5 a     d         5
 6 a     d         6
 7 a     d         7
 8 a     d         8
 9 a     d         9
10 a     d        10
# … with 530 more rows

If we need to split

mydf %>%
    group_by(y, z) %>%
    summarise(x = 1:180, .groups = 'keep') %>%
    group_split()

-output

[[1]]
# A tibble: 180 x 3
   y     z         x
   <chr> <chr> <int>
 1 a     d         1
 2 a     d         2
 3 a     d         3
 4 a     d         4
 5 a     d         5
 6 a     d         6
 7 a     d         7
 8 a     d         8
 9 a     d         9
10 a     d        10
# … with 170 more rows

[[2]]
# A tibble: 180 x 3
   y     z         x
   <chr> <chr> <int>
 1 b     e         1
 2 b     e         2
 3 b     e         3
 4 b     e         4
 5 b     e         5
 6 b     e         6
 7 b     e         7
 8 b     e         8
 9 b     e         9
10 b     e        10
# … with 170 more rows

[[3]]
# A tibble: 180 x 3
   y     z         x
   <chr> <chr> <int>
 1 c     f         1
 2 c     f         2
 3 c     f         3
 4 c     f         4
 5 c     f         5
 6 c     f         6
 7 c     f         7
 8 c     f         8
 9 c     f         9
10 c     f        10
# … with 170 more rows

Or another option is complete

library(tidyr)
 mydf %>%
    group_by(y, z) %>% 
    complete(x = 1:180) %>%
    ungroup
  •  Tags:  
  • r
  • Related