2

I have a dataset with date values like this.

      Date        Id
      10/1/2013   3444
                  87873
                  1243
                  35473
                  7363
                  8335 
      10/2/2013   4783
                  3355
                  4544
                  94453
                  33554

What I am trying to do is automatically populate the date values until the next date value is encountered, Oct 1 2013 should be populated until Oct 2 2013 is encountered. The final dataset should look like this

      Date        Id
      10/1/2013   3444
      10/1/2013   87873
      10/1/2013   1243
      10/1/2013   35473
      10/1/2013   7363
      10/1/2013   8335 
      10/2/2013   4783
      10/2/2013   3355
      10/2/2013   4544
      10/2/2013   94453
      10/2/2013   33554

Any help is appreciated.

Ty Voss
  • 283
  • 5
  • 12

1 Answers1

4

We could try na.locf from zoo after changing the '' values to NA.

library(zoo)
is.na(df1$Date) <- df1$Date==''
df1$Date <- na.locf(df1$Date)

Or using data.table, we convert the 'data.frame' to 'data.table' (setDT(df1)), for the grouping variable (cumsum(Date!='')), we assign (:=) 'Date' as the first element of 'Date' (Date[1L])

library(data.table)
setDT(df1)[, Date:= Date[1L] , cumsum(Date!='')]

data

 df1 <- structure(list(Date = c("10/1/2013", "", "", "", "", "", 
 "10/2/2013", 
 "", "", "", ""), Id = c(3444L, 87873L, 1243L, 35473L, 7363L, 
 8335L, 4783L, 3355L, 4544L, 94453L, 33554L)), .Names = c("Date", 
 "Id"), class = "data.frame", row.names = c(NA, -11L))
akrun
  • 874,273
  • 37
  • 540
  • 662