首页 > 解决方案 > 在 dplyr 中重复行并相应地生成一个新列

问题描述

我有一个像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)

我想重复每个观察 11 次(每个月一次)。所以我们有类似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)

要重复我尝试过的每一列:

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

但是,我还必须以正确的方式生成月份函数。

有什么线索吗?

标签: rrowrepeatdplyr

解决方案


您可以使用uncount重复每行 12 次并使用 - 创建月份列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

推荐阅读