R与Stata的fillin等效吗?

时间:2019-05-31 00:39:17

标签: r stata

Stata的fillin命令使数据集呈矩形。 如何在 R 中执行相同的操作?

作为可重复的示例,假设我有以下数据:

data <- data.frame(season = c("2016-17","2017-18","2018-19", "2018-19"), group=c("A","A","A","B"), value=c(1,2,3,6)) 

我想得到以下信息:

data_wanted <- data.frame(season = c("2016-17","2017-18","2018-19", "2018-19", "2016-17", "2017-18"), group=c("A","A","A","B","B","B"),value=c(1,2,3,6,NA,NA))

1 个答案:

答案 0 :(得分:1)

使用tidyr::complete,只需指定您想要季节和分组的每种组合即可:

tidyr::complete(data, season, group)