在此处输入代码
in_time <- read.csv('in_time.csv', stringsAsFactors = F)
out_time <- read.csv('out_time.csv', stringsAsFactors = F)
for(i in 2:length(colnames(in_time)) ) {
in_time[i] <- strptime(in_time[i], format="%d-%m-%Y %H:%M")
}
for(i in 2:length(colnames(out_time)) ) {
out_time[i] <- strptime(out_time[i], format="%d-%m-%Y %H:%M")
}
#time_difference
for(i in 2:length(colnames(out_time)) ) {
time_in_office[i] <- out_time[i] - in_time[i]
}
我无法获得时差 in_time 和out_time文件包含除第一列以外的日期时间值
您可以使用lubridate
包来计算传入和传出事件的时差。要计算in_time和out_time的时间差,您可以使用mapply
函数。请参阅下面的代码:
in_time <- structure(list(EmployeeID = 1:3, X02.01.2015 = c("02-01-2015 09:43",
"02-01-2015 10:15", "02-01-2015 10:17"), X05.01.2015 = c("05-01-2015 10:08",
"05-01-2015 10:21", "05-01-2015 09:50"), X06.01.2015 = c("06-01-2015 09:54",
"07-01-2015 09:45", "06-01-2015 10:14"), X07.01.2015 = c("07-01-2015 09:34",
NA, NA)), class = "data.frame", row.names = c(NA, -3L))
out_time <- structure(list(EmployeeID = c(1, 2, 3), X02.01.2015 = c("02-01-2015 19:43",
"02-01-2015 18:15", "02-01-2015 17:17"), X05.01.2015 = c("05-01-2015 15:02",
"05-01-2015 12:11", "05-01-2015 21:50"), X06.01.2015 = c("06-01-2015 16:34",
"07-01-2015 19:45", "06-01-2015 18:14"), X07.01.2015 = c("07-01-2015 21:34",
"NA", "NA")), row.names = c(NA, 3L), class = "data.frame")
library(lubridate)
# converse datetime into POSIXct format
in_time[, -1] <- lapply(in_time[, -1], dmy_hm)
out_time[, -1] <- lapply(out_time[, -1], dmy_hm)
# calculate time difference
result <- in_time
result[, -1] <- mapply(function(x, y) {
mapply(function(x, y) as.numeric(as.period(interval(x, y)), unit = "hours"), x, y)
}, in_time[,-1], out_time[,-1])
result
输出:员工在工作上花费的时间,以小时为单位:
EmployeeID X02.01.2015 X05.01.2015 X06.01.2015 X07.01.2015
1 1 10 4.900000 6.666667 12
2 2 8 1.833333 10.000000 NA
3 3 7 12.000000 8.000000 NA