data.table outer join by group

前端 未结 1 1183
北海茫月
北海茫月 2021-01-06 00:06

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

相关标签:
1条回答
  • 2021-01-06 01:04

    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
    
    0 讨论(0)
提交回复
热议问题