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

前端 未结 4 1915
别那么骄傲
别那么骄傲 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:09

    Hers's a dplyr solution. The result, based on the sample data, is a data.frame with 89 rows, I hope that's what you intended to get.

    require(dplyr)
    
    PostData %>%
      mutate(Date = as.Date(as.character(Date))) %>%
      group_by(IndID) %>%
      do(left_join(data.frame(IndID = .$IndID[1], Date = seq(min(.$Date), max(.$Date), 1)), ., 
                           by=c("IndID", "Date"))) %>%
      mutate(Event = ifelse(is.na(Event), 0, Event))
    
    #   IndID       Date Event Number Percent
    #1    P01 2011-03-04     1      2   0.390
    #2    P01 2011-03-05     0     NA      NA
    #3    P01 2011-03-06     0     NA      NA
    #4    P01 2011-03-07     0     NA      NA
    #5    P01 2011-03-08     0     NA      NA
    #6    P01 2011-03-09     0     NA      NA 
    #7    P01 2011-03-10     0     NA      NA
    #8    P01 2011-03-11     1      2   0.975
    #...
    #84   P06 2012-01-25     0     NA      NA
    #85   P06 2012-01-26     0     NA      NA
    #86   P06 2012-01-27     1      4   0.758
    #87   P06 2012-01-28     0     NA      NA
    #88   P06 2012-01-29     0      8   0.290
    #89   P06 2012-01-30     0      1   0.150
    

提交回复
热议问题