0

I want to create function which handles question: 05:00:00 - 28:59:59 time format

When I run script from that question it's working but when I run the function:

 pal <- NormalDates(data,Date,Time.start)
Error in unclass(e1) + unclass(e2) : 
  non-numeric argument to binary operator
Called from: `+.POSIXt`(as.POSIXct(as.character(df$day), format = "%d.%m.%Y"), 
    sapply(strsplit(as.character(df$time), ":"), function(t) {
        t <- as.integer(t)
        t[3] + t[2] * 60 + t[1] * 60 * 60
    }))
Browse[1]>  

How can I get rid of this error?

My function:

NormalDates <- function(df,day,time) {
    # Function transforms 05:00:00 - 28:59:59 time to usual date vector
    #
    # Args: 
    #    df: data frame that contains  datasheet
    #    date: column name with date
    #    time: column name with time
    #
    # Returns:
    #    Vector with normal dates of class "POSIXct"
    
    # Error handling
if (class(df) != "data.frame") {
    stop("Not a data.frame object")
} 

arguments <- as.list(match.call())
day = eval(arguments$day, df)
time = eval(arguments$time, df)

#New date vector generating
pal <- as.POSIXct(as.character(df$day), format="%d.%m.%Y") +
    sapply(strsplit(as.character(df$time), ":"), function(t) {
        t <- as.integer(t)
        t[3] + t[2] * 60 + t[1] * 60 * 60
    })
return(pal)
}
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
BiXiC
  • 933
  • 3
  • 9
  • 29
  • Do some basic debugging. E.g., let the function only return the `sapply(strsplit(...` part to see if there is something unexpected there. – Roland Jun 19 '14 at 14:30
  • Looks complicated. Why not just `NormalDates(df, "Date", "Time.start")` with `df[, day]` instead of `df$day` and `df[, time]` instead of `df$time`? – lukeA Jun 19 '14 at 14:35

0 Answers0