r - 在 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), ]
但是,我还必须以正确的方式生成月份函数。
有什么线索吗?
解决方案
您可以使用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
推荐阅读
- php - 如何在 wordpress 的自定义字段中禁用类别?
- html - 如何从 Angular 表单中获取与另一个表映射为多对多的列的输入?
- node.js - NodeMailer,发送特定数量的电子邮件
- heroku - netlify 上的 React 前端无法连接到数据库
- javascript - 如何更改渲染的组件?
- python - ModuleNotFoundError:Windows 上没有名为“grp”的模块
- php - VIVVO Lib 框架问题
- r - 带有百分比的堆积条形图
- javascript - 使用 Javascript 在前端创建基本 URL(开发、API 和生产)
- java - 拆分字符串没有给出预期的输出