-1

The data below have two general groups that contain multiple observations, some of which are NA in the DLA field. The DLA date is the same for all records within a group. How can I expand the DLA value to 'fill in' the NA values with the respective date. I am working within dplyr, which I suspect has a solution that I have not been able to locate. These data are a small subset of a larger data set with ~5k rows and ~500 individuals. Many thanks.

dat <- structure(list(GenIndID = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("BHS_106", 
"BHS_164"), class = "factor"), IndID = structure(c(1L, 1L, 1L, 
1L, 2L, 2L, 2L, 2L, 3L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 7L, 
8L), .Label = c("BHS_106_A", "BHS_106_B", "BHS_106_C", "BHS_106_D", 
"BHS_164_A", "BHS_164_B", "BHS_164_C", "BHS_164_D"), class = "factor"), 
    DLA = structure(c(1507010400, 1507010400, 1507010400, 1507010400, 
    1507010400, 1507010400, 1507010400, 1507010400, NA, NA, 1499061600, 
    1499061600, 1499061600, 1499061600, 1499061600, 1499061600, 
    1499061600, NA, NA, NA), tzone = "", class = c("POSIXct", 
    "POSIXt"))), .Names = c("GenIndID", "IndID", "DLA"), row.names = c(411L, 
412L, 413L, 414L, 415L, 416L, 417L, 418L, 419L, 420L, 442L, 443L, 
444L, 445L, 446L, 447L, 448L, 449L, 450L, 451L), class = "data.frame")

> dat
    GenIndID     IndID        DLA
411  BHS_106 BHS_106_A 2017-10-03
412  BHS_106 BHS_106_A 2017-10-03
413  BHS_106 BHS_106_A 2017-10-03
414  BHS_106 BHS_106_A 2017-10-03
415  BHS_106 BHS_106_B 2017-10-03
416  BHS_106 BHS_106_B 2017-10-03
417  BHS_106 BHS_106_B 2017-10-03
418  BHS_106 BHS_106_B 2017-10-03
419  BHS_106 BHS_106_C       <NA>
420  BHS_106 BHS_106_D       <NA>
442  BHS_164 BHS_164_A 2017-07-03
443  BHS_164 BHS_164_A 2017-07-03
444  BHS_164 BHS_164_A 2017-07-03
445  BHS_164 BHS_164_A 2017-07-03
446  BHS_164 BHS_164_A 2017-07-03
447  BHS_164 BHS_164_A 2017-07-03
448  BHS_164 BHS_164_A 2017-07-03
449  BHS_164 BHS_164_B       <NA>
450  BHS_164 BHS_164_C       <NA>
451  BHS_164 BHS_164_D       <NA>
B. Davis
  • 3,391
  • 5
  • 42
  • 78
  • 2
    Possible duplicate of [How to replace NA with most recent non-NA by group?](https://stackoverflow.com/questions/39063253/how-to-replace-na-with-most-recent-non-na-by-group) OR [replace NA value with the group value](https://stackoverflow.com/questions/23583739/replace-na-value-with-the-group-value) – Ronak Shah Oct 12 '17 at 06:22
  • Yep, this is a duplicate. Apologies. How can I delete? SO will not allow ( at least with my reputation) given it has an answer. – B. Davis Oct 12 '17 at 15:07

1 Answers1

0

We need the fill after grouping by 'GenIndID'. As the NAs are at the bottom, the .direction = 'down' by default. So, we don't need to specify it

dat %>%
  group_by(GenIndID) %>% 
  fill(DLA)
akrun
  • 874,273
  • 37
  • 540
  • 662