05:00:00 - 28:59:59 time format function

旧时模样 提交于 2020-01-07 08:22:31

问题


I want to create function wich 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)
}

来源:https://stackoverflow.com/questions/24308718/050000-285959-time-format-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!