我会使用expand.grid
and merge
。
假设您的数据如下:
mydf <- structure(list(FIRM = c("FIRM.1", "FIRM.1", "FIRM.2", "FIRM.2"),
YEAR = c("YEAR.1", "YEAR.3", "YEAR.2", "YEAR.3"), VAR = c("x.1", "x.2",
"x.3", "x.4")), .Names = c("FIRM", "YEAR", "VAR"),
class = "data.frame", row.names = c(NA, -4L))
mydf
# FIRM YEAR VAR
# 1 FIRM.1 YEAR.1 x.1
# 2 FIRM.1 YEAR.3 x.2
# 3 FIRM.2 YEAR.2 x.3
# 4 FIRM.2 YEAR.3 x.4
用于expand.grid
创建“完整”集的“FIRM”和“YEAR”数据,然后merge
.
merge(mydf, expand.grid(FIRM = unique(mydf$FIRM),
YEAR = unique(mydf$YEAR)),
all.y = TRUE)
# FIRM YEAR VAR
# 1 FIRM.1 YEAR.1 x.1
# 2 FIRM.1 YEAR.2 <NA>
# 3 FIRM.1 YEAR.3 x.2
# 4 FIRM.2 YEAR.1 <NA>
# 5 FIRM.2 YEAR.2 x.3
# 6 FIRM.2 YEAR.3 x.4