Search code examples
rrowrepeatdplyr

Repeat rows and generate a new column accordingly in dplyr


I have a dataset like countries_not_appear_indices

countries_not_appear_indices <- as.data.frame(c("CUW", "ARM", "VGB", "ATG", "KNA", "GRD", "VCT", "LCA", "TCA", "GNB", "GNQ", "CYM", "MNE", "MDV", "MKD", "GIB", "LIE", "COM", "NCL", "BES", "PYF")) %>%
  rename(iso = `c("CUW", "ARM", "VGB", "ATG", "KNA", "GRD", "VCT", "LCA", "TCA", "GNB", "GNQ", "CYM", "MNE", "MDV", "MKD", "GIB", "LIE", "COM", "NCL", "BES", "PYF")`) %>%
  mutate(ie = 0, ih = 0)

and I want to repeat each observation 11 times (one time for each month). So we have something like countries_not_appear_indices_panel

iso <- c("CUW", "CUW", "CUW", "CUW", "CUW", "CUW", "CUW", "CUW", "CUW", "CUW", "CUW", "CUW", "ARM", "ARM", "ARM", "ARM", "ARM", "ARM", "ARM", "ARM", "ARM", "ARM", "ARM", "ARM")
month <- c(1,2,3,4,5,6,7,8,9,10,11,12,1,2,3,4,5,6,7,8,9,10,11,12)
ie <- c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
ih <- c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)

countries_not_appear_indices_panel <- data.frame(iso, month, ie, ih)

To repeat each column I did try:

countries_not_appear_indices[rep(seq_len(nrow(countries_not_appear_indices)), each = 12), ]

However, I also have to generate the month function in the correct way.

Any clue?


Solution

  • You can use uncount to repeat each row 12 times and create month column using row_number() -

    library(dplyr)
    library(tidyr)
    
    countries_not_appear_indices %>%
      uncount(12) %>%
      group_by(iso) %>%
      mutate(month = row_number(), .after = 'iso') %>%
      ungroup 
    
    #   iso   month    ie    ih
    #   <chr> <int> <dbl> <dbl>
    # 1 CUW       1     0     0
    # 2 CUW       2     0     0
    # 3 CUW       3     0     0
    # 4 CUW       4     0     0
    # 5 CUW       5     0     0
    # 6 CUW       6     0     0
    # 7 CUW       7     0     0
    # 8 CUW       8     0     0
    # 9 CUW       9     0     0
    #10 CUW      10     0     0
    # … with 242 more rows