Search code examples
rdatestrptimeposixlt

how to set the day, month and year in a posixlt in r?


i have to fix a date in a data frame. the date comes in as "16/12/2006", and the time comes in as "17:24:00". i want to construct a posixlt with the combined date and time. i tried:

fixTime2<-function(date,time) { # replaces the date in time with the parameter date.
    fixed<-time
    fixed$mon<-as.numeric(format(date,"%m"))
    fixed$mday<-as.numeric(format(date,"%d"))
    fixed$year<-as.numeric(format(date,"%Y"))-1900
    return(fixed)
}
testFixTime2<-function() {
    date<-as.Date("16/12/2006", format = "%d/%m/%Y")
    str(date)
    time<-strptime("17:24:00","%H:%M:%S")
    str(time)
    fixed<-fixTime2(date,time)
    str(fixed)
    return(fixed)
}

when i run the program, i get:

> source("1.R")
> f<-testFixTime2()
 Date[1:1], format: "2006-12-16"
 POSIXlt[1:1], format: "2018-07-17 17:24:00"
 POSIXlt[1:1], format: "2007-01-16 17:24:00"

the year is off by one, and the day and month are incorrect.

i have tried $month, and $day but they do not seem to work either.

is there an easier way to construct a posixlt?

thanks

i ended up with:

require(RUnit)
fixTime<-function(date,time) { # replaces the date in time with the paramer date.
    as.POSIXlt(sprintf("%s %s",date,time),format="%d/%m/%Y %H:%M:%S")
}
fixTime2<-function(month,day,year,hours,minutes,seconds) {
    print("in function fixTime")
    print(year)
    date<-paste0(c(day,month,year),collapse="/")
    time<-paste0(c(hours,minutes,seconds),collapse=":")
    fixed<-fixTime(date,time)
}
test.fixTime<-function() {
    month<-"12"
    day<-"16"
    year<-"2006"
    hours<-"17"
    minutes<-"24"
    seconds<-"00"
    print(year)
    fixTime2(month,day,year,hours,minutes,seconds)
    #fixed<-fixTime(date,time)
    checkEquals(as.numeric(month),fixed$mon+1)
    checkEquals(as.numeric(day),fixed$mday)
    checkEquals(as.numeric(year),fixed$year+1900)
    checkEquals(as.numeric(hours),fixed$hour)
    checkEquals(as.numeric(minutes),fixed$min)
    checkEquals(as.numeric(seconds),fixed$sec)
}

this is homework for one of the coursera r courses.


Solution

  • If you have individual strings, then

    as.POSIXlt(paste(date, time), format="%d/%m/%Y %H:%M:%S")
    

    should work. Example:

    as.POSIXlt(paste("16/12/2006", "17:24:00"), format="%d/%m/%Y %H:%M:%S")
    # [1] "2006-12-16 17:24:00"
    

    For your second point, $month and $day aren't available attributes. You can see the attributes available with something like:

    dput(as.POSIXlt(Sys.time()))
    # structure(list(sec = 48.7993450164795, min = 17L, hour = 0L, 
    #     mday = 18L, mon = 6L, year = 118L, wday = 3L, yday = 198L, 
    #     isdst = 1L, zone = "PDT", gmtoff = -25200L), .Names = c("sec", 
    # "min", "hour", "mday", "mon", "year", "wday", "yday", "isdst", 
    # "zone", "gmtoff"), class = c("POSIXlt", "POSIXt"), tzone = c("", 
    # "PST", "PDT"))
    

    showing that what you really need are $mon and $mday.