R:从日期和时间计算总时数

时间:2018-10-03 17:04:55

标签: r calculated-columns date-arithmetic

第一次问及R的新问题。

我正在从SQL Server中提取数据并将其放入R数据表(SAP)。我正在尝试从4列(StartDate,FinDate,StartTime,FinTime)计算总小时数。

DataTable

我已经尝试过从数据表(SAP)中计算出结果,但没有得到我想要的。

SAP$hours <- with(SAP,
                  difftime(c(ActStartDate, ActStartTime), 
                           c(ActFinDate, ActFinTime),
                           units = "hours") )    

我想将总时数添加到数据表中,或者将一个向量分配给总时数。

这就是我在excel中的做法:

小时数=(((结束日期+结束时间)-(开始日期+开始时间))* 24

2 个答案:

答案 0 :(得分:1)

您可以执行以下操作:

#sample data:
df <- data.frame(startdate = c("2018-08-23 00.00.00"), 
                 enddate = c("2018-08-24 00.00.00"), 
                 starttime = c("23:00:00"), 
                 endtime = c("23:30:00"))

#This will first combine date(after extracting the date part) and time and
#then convert it to a date time object readable by R. 
df$sdt <- as.POSIXct(paste(substr(df$startdate, 1, 10), 
                           df$starttime, 
                           sep = " "), 
                     format = "%Y-%m-%d %H:%M:%S")    

#Same for end date time
df$edt <- as.POSIXct(paste(substr(df$enddate, 1, 10), 
                           df$endtime, 
                           sep = " "), 
                     format = "%Y-%m-%d %H:%M:%S")

df$diff <- difftime(df$edt, df$sdt, units = "hours")

答案 1 :(得分:0)

谢谢大家。我最终用您的输入完成了此操作,并成功了。

    # make it a data table
    SAP <- data.table(SAP)

    # only select some columns of interest
    SAP <- SAP[, .(Equipment, Order, ActStartDate, ActStartTime, ActFinDate, ActFinTime)]

    # generate start / end as POSIX, 
    # this code assumes that start date from SAP is always like 2018-05-05
    # so 10 chars as YYYY-MM-DD
    # if needed add time zone information, e.g as.POSIXct(..., tz = 'UTC')
    SAP[, start := as.POSIXct(paste0(substring(ActStartDate, 1, 10), ActStartTime))]
    SAP[, end := as.POSIXct(paste0(substring(ActFinDate, 1, 10), ActFinTime))]

    # calculate duration
    SAP[, duration := difftime(end, start, units = "hours")]