Search code examples
rxts

Using date and seconds from midnight columns to convert a data.frame to an xts object (R)


Suppose I have data that looks like this:

        DATE  TIME   Col1   Col2
1 1993-01-04 34538 10.250 10.000
2 1994-01-05 34541 10.250 10.111
3 1997-03-16 34546 10.250 10.222
4 2017-11-10 34561 10.251 10.333
5 2001-08-28 34565 10.251 10.444
6 2006-04-20 34807 10.251 10.555

The 'TIME' column is formatted as seconds from midnight. How would I combine the 'DATE' and 'TIME' columns to get an xts object that looks something like this:

                       Col1   Col2
X1993.01.04.09.35.38 10.250 10.000
X1994.01.05.09.35.41 10.250 10.111
X1997.03.16.09.35.46 10.250 10.222
X2017.11.10.09.36.01 10.251 10.333
X2001.08.28.09.36.05 10.251 10.444
X2006.04.20.09.40.07 10.251 10.555

Solution

  • We can convert the 'DATE', 'TIME' column to Datetime class and convert the dataset to xts by specifying the order.by

    library(xts)     
    library(lubridate)
    xts(df1[-(1:2)], order.by = as.POSIXct(paste(df1$DATE, 
                 hms::hms(seconds_to_period(df1$TIME)))))
    #                      Col1   Col2
    #1993-01-04 09:35:38 10.250 10.000
    #1994-01-05 09:35:41 10.250 10.111
    #1997-03-16 09:35:46 10.250 10.222
    #2001-08-28 09:36:05 10.251 10.444
    #2006-04-20 09:40:07 10.251 10.555
    #2017-11-10 09:36:01 10.251 10.333
    

    NOTE: The index of xts needs a Datetime class object and not a formatted character class vector

    data

    df1 <- structure(list(DATE = c("1993-01-04", "1994-01-05", "1997-03-16", 
    "2017-11-10", "2001-08-28", "2006-04-20"), TIME = c(34538L, 34541L, 
    34546L, 34561L, 34565L, 34807L), Col1 = c(10.25, 10.25, 10.25, 
    10.251, 10.251, 10.251), Col2 = c(10, 10.111, 10.222, 10.333, 
    10.444, 10.555)), class = "data.frame", row.names = c("1", "2", 
    "3", "4", "5", "6"))