Search code examples
rdateposixlt

Convert factor to POSIXlt with propagating date in R


I have a vector with time information and a date noted only once per day. I need to convert the vector into a usable format such as POSIXlt. The times are ordered, where all times (%H:%M) within a day belong to the last date noted before the date-less time.

t <- structure(c(6L, 1L, 2L, 3L, 4L, 5L, 10L, 7L, 8L, 9L), 
    .Label = c("00:15", "00:25", "00:35", "00:45", "02:05", "20.01.2013; 0:05", 
    "20:48", "20:58", "21:08", "25.01.2013; 20:38"), class = "factor")

From multiple previous answers to questions about factor to date conversion (e.g. here), I know how to convert t[c(1, 7)].

t1 <- strptime(as.character(t[c(1, 7)]), format = "%d.%m.%Y; %H:%M")
# t1
# [1] "2013-01-20 00:05:00 CET" "2013-01-25 20:38:00 CET"

However, how can I propagate the missing date for the remaining values so that they would convert correctly?


Solution

  • library(zoo)  # For the na.locf function
    
    df = data.frame(date=t)
    
    # Put date and time in separate columns
    df$time = gsub(".*; (.*)","\\1", df$date)
    df$date = as.Date(df$date, format="%d.%m.%Y")
    
    # Fill missing df$date values
    df$date = na.locf(df$date)
    
    # Convert to POSIXct
    df$date = as.POSIXct(paste(df$date, df$time))
    df = df[,1, drop=FALSE]
    
    df
    
                      date
    1  2013-01-20 00:05:00
    2  2013-01-20 00:15:00
    3  2013-01-20 00:25:00
    4  2013-01-20 00:35:00
    5  2013-01-20 00:45:00
    6  2013-01-20 02:05:00
    7  2013-01-25 20:38:00
    8  2013-01-25 20:48:00
    9  2013-01-25 20:58:00
    10 2013-01-25 21:08:00