Adding missing date values in a data frame with multiple observation periods

前端 未结 4 1917
别那么骄傲
别那么骄傲 2021-01-13 12:39

Thanks in advance.

I am trying to add missing date values that were not included in a observation period for three different individuals.

My data look like t

4条回答
  •  天涯浪人
    2021-01-13 13:07

    Calculate min and max times (seconds since Epoch):

    min_time = as.integer(min(PostData$Date))
    max_time = as.integer(max(PostData$Date))
    

    Use sequence to build the list of missing dates:

    list_of_dates = seq(min_time,max_time, 86400) #since there are 86400 seconds in a day
    list_of_dates = as.Date(as.POSIXct( list_of_dates ), origin = '1970-01-01 00:00.00 UTC') 
    #convert back to a date
    

    Build a list of missing IndID and Date combos

    temp = merge(unique(PostData$IndID),list_of_dates)
    names(temp) = c("IndID","Date")
    data_missing_indID_date = temp[!which(temp$IndID %in% PostData$IndID & temp$Date %in% PostData$Date ),]
    

    Build the rest of the columns:

    data_missing_indID_date$Event = 0 
    data_missing_indID_date$Number = NA
    data_missing_indID_date$Percent = NA
    

    rbind it to the original data frame:

    final_data = rbind(PostData, data_missing_indID_date)
    

提交回复
热议问题