R remove rows from panel while keeping the panel balanced

后端 未结 4 683
面向向阳花
面向向阳花 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 01:57

    This is the solution that I use - it makes use of the convenient features (including nice merging abilities) of the data.table package and assumes that your data is already a data.table object. It is relatively simple and hopefully easy to follow. It returns a balanced panel with entries for every unique combination of the "individuals" and the "time periods" - i.e. a panel where there is an observation for every individual in every time period.

    library(data.table)
    Balance_Panel = function(Data, Indiv_ColName, Time_ColName){
        Individuals = unique(Data[, get(Indiv_ColName)])
        Times = unique(Data[, get(Time_ColName)])
    
        Full_Panel = data.table(expand.grid(Individuals, Times))
        setnames(Full_Panel, c(Indiv_ColName, Time_ColName))
        setkeyv(Full_Panel, c(Indiv_ColName, Time_ColName))
        setkeyv(Data, c(Indiv_ColName, Time_ColName))
        return(Data[Full_Panel])
    }
    

    Sample Usage:

    Balanced_Data = Balance_Panel(Data, "SubjectID", "ObservationTime")
    

提交回复
热议问题