Search code examples
rdatedatetimeposixctposixlt

Converting combined Day/Time decimal format to POSIX


I have a data set that I believe is given in 20 minute increments. The year and month columns are given in a standard decimal value. But the the Day and Hour/Min/Sec data are given as a combined decimal (First digit is "Day", following decimal places are time). As example:

Year      month decimal.day
2014       1      1.0139
2014       1      1.0278
2014       1      1.0417
2014       1      1.0556
2014       1      1.0694
2014       1      1.0833
2014       1      1.0972
2014       1      1.1111
2014       1      1.1250
2014       1      1.1389
2014       1      1.1528
2014       1      1.1667
2014       1      1.1806
2014       1      1.1944
  .        .      .
  .        .      .
  .        .      .
  .
 2014      1      1.9722
 2014      1      1.9861
 2014      1      2.0000
 2014      1      2.0139
 2014      1      2.0278
 2014      1      2.0417
 2014      1      2.0556
 2014      1      2.0694
 2014      1      2.0833

Using R, is it possible to convert the decimal.day column into a standard time format? In the end I would like to concatenate all 3 columns into a standard POSIX "YYYY-MM-DD HH:MM:SS" format.


Solution

  • You can use as.POSIXct() to do so:

    as.POSIXct.numeric((df$decimal.day-1)*24*3600, 
                       origin=paste(df$Year, df$month, 1, sep = "/"), tz="GMT")
    

    Note the -1 to balance starting the first day of the month