R remove rows from panel while keeping the panel balanced

后端 未结 4 682
面向向阳花
面向向阳花 2021-01-01 01:29

Is there an elegant way to balance an unbalanced panel data set? I would like to start with an unbalanced panel (ie, some individuals are missing some data) and end up with

4条回答
  •  滥情空心
    2021-01-01 02:06

    One way to balance a panel is to remove individuals with incomplete data, another way is to fill in a value, such as NA or 0 for the missing observations. For the first approach, you can use complete.cases to find rows that have no NA in them. Then you can find all the PERSON with at least one missing case.

    missing.at.least.one <- unique(unbal$PERSON[!complete.cases(unbal)])
    unbal[!(unbal$PERSON %in% missing.at.least.one),]
    #    PERSON YEAR  Y  X
    # 1   Frank 2001 21  1
    # 2   Frank 2002 22  2
    # 3   Frank 2003 23  3
    # 4   Frank 2004 24  4
    # 5   Frank 2005 25  5
    # 11 Edward 2001 31 11
    # 12 Edward 2002 32 12
    # 13 Edward 2003 33 13
    # 14 Edward 2004 34 14
    # 15 Edward 2005 35 15
    

提交回复
热议问题