I am trying to use data.table to fill missing observations in a large unbalanced multi-dimensional panel that I have. Below is an example of the data with some comments as t
It's always good to post the answer you're expecting as well to avoid any ambiguity/miscommunication. Is this what you're looking for?
require(data.table)
dt <- as.data.table(mydat)
setkey(dt, "yearqtr")
dt[, .SD[J(allqtrs)], by = list(fund, holdingid)]
# fund holdingid yearqtr shares
# 1: 1 10 2000.00 20
# 2: 1 10 2000.25 NA
# 3: 1 10 2000.50 25
# 4: 1 10 2000.75 NA
# 5: 1 11 2000.00 30
# 6: 1 11 2000.25 30
# 7: 1 11 2000.50 NA
# 8: 1 11 2000.75 NA
# 9: 2 15 2000.00 34
# 10: 2 15 2000.25 NA
# 11: 2 15 2000.50 NA
# 12: 2 15 2000.75 34
# 13: 2 14 2000.00 NA
# 14: 2 14 2000.25 4
# 15: 2 14 2000.50 NA
# 16: 2 14 2000.75 NA
# 17: 3 20 2000.00 NA
# 18: 3 20 2000.25 8
# 19: 3 20 2000.50 10
# 20: 3 20 2000.75 NA