Created
April 30, 2016 00:37
-
-
Save vikjam/3db6249ba2ab624e5bd84570bb8b6d03 to your computer and use it in GitHub Desktop.
Pads an imbalanced panel data set with missing values.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # via http://www.enoriver.net/2015/02/09/r-version-of-tsfill-and-xfill-combined/ | |
| xtill <- function(x) { | |
| dt <- copy(x) | |
| xtkeys <- c('zip.cd','cal.dt') | |
| clkeys <- c('zip.cd','dma.cd') | |
| xtfull <- CJ(unique(x[,zip.cd]),unique(x[,cal.dt])) | |
| clfull <- unique(subset(x,select=c(zip.cd,dma.cd))) | |
| setnames(xtfull,c('V1','V2'),xtkeys) | |
| setkeyv(xtfull,xtkeys) | |
| setkeyv(dt,xtkeys) | |
| setkeyv(clfull,clkeys) | |
| xtfull <- subset(merge(xtfull,dt,all=TRUE),select=-dma.cd) | |
| xtfull <- merge(xtfull,clfull,all=TRUE) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment