-->

filling in missing data in one data frame with inf

2020-08-02 06:27发布

问题:

There are two data set, A & B, as below:

A <- data.frame(TICKER=c("00EY","00EY","00EY","00EY","00EY"), 
                CUSIP=c(NA,NA,"48205A10","48205A10","48205A10"), 
                OFTIC=c(NA,NA,"JUNO","JUNO","JUNO"), 
                CNAME=c(NA,NA, "JUNO", "JUNO","JUNO"), 
                ANNDATS=c("2015-01-13","2015-01-13","2015-01-13","2015-01-13","2015-01-13"),
                ANALYS=c(00076659,00105887,00153117,00148921,00086659),
                stringsAsFactors = F)

B <- data.frame(TICKER=c("00EY","00EY","00EY","00EY"), 
                CUSIP=c("48205A10","48205A10","48205A10","48205A10"),
                OFTIC=c("JUNO","JUNO",NA,NA), 
                CNAME=c("JUNO","JUNO", NA, NA), 
                ANNDATS=c("2015-01-13","2015-01-13","2015-01-13","2015-01-13"), 
                ANALYS=c(00076659,00105887,00153117,00148921), 
                stringsAsFactors = F)

How can I fill in missing data in one data frame with info from another? (A & B data sets are not of the same length).

回答1:

Since the two data sets can have different lengths, you need some feature which they can be connected by. As it seems ANALYS is some kind of identifier, we can use it to connect the two data.frames in this example.

First we identify all missings in df1 (i.e. A) and acquire their indices (rows and cols).
Then, the missings in df1 are subsituted by the values in df2 corresponding to the line with the same value of ANALYS. If this ID is not available in df2, the line will be skipped.

f <- function(df1, df2){
  missings <- sapply(df1, is.na)
  missingsInd <- which(missings, arr.ind = T)

  for(i in 1:nrow(missingsInd)){
    idOfMissing <- df1$ANALYS[missingsInd[i,1]]
    correspondingLine <- df2[which(df2$ANALYS == idOfMissing), ]
    if (nrow(correspondingLine) != 0) {
      df1[missingsInd[i,1], missingsInd[i,2]] <-  correspondingLine[1,missingsInd[i,2]]
    }
  }
  df1
}
f(A, B)
#   TICKER    CUSIP OFTIC CNAME    ANNDATS ANALYS IRECCD
# 1   00EY 48205A10  JUNO  JUNO 2015-01-13  76659      1
# 2   00EY 48205A10  JUNO  JUNO 2015-01-13 105887      2
# 3   00EY 48205A10  JUNO  JUNO 2015-01-13 153117      1
# 4   00EY 48205A10  JUNO  JUNO 2015-01-13 148921      3
# 5   00EY 48205A10  JUNO  JUNO 2015-01-13  86659      4

Note that cells with NAs within both data.frames will return as NA as in the output. Furthermore, this only applies if ANALYS only holds unique values within both data.frames.