最后我为此写了一个函数,我知道这个函数很丑
并没有优化,但这是目前我可以得到的结果,如果有人能提供一个更简单的解决方案,那就太好了。
getp1TOp2 <- function(x,y,p1,p2) {
#Calcuate the numofdays
numdays <- as.numeric(difftime( update(as.POSIXct(y), hour=0, min=0, sec=0), update(as.POSIXct(x), hour=0, min=0, sec=0),units = "days"))
#Calculate the FirstdayStart, FirstdayEnd, LastdayStart and LastdayEnd
FirstdayStart <- update(as.POSIXct(x), hour=p1, min=0, sec=0)
FirstdayEnd <- update(as.POSIXct(x), hour=p2, min=0, sec=0)
LastdayStart <- update(as.POSIXct(y), hour=p1, min=0, sec=0)
LastdayEnd <- update(as.POSIXct(y), hour=p2, min=0, sec=0)
# For case Start and End in the same day , eg (2015-09-07 03:00, 2015-09-07 06:00)
if (numdays==0)
{
#For case StartTime >= FirstdayStart
if (as.POSIXct(x) >= FirstdayStart)
{
#For case EndTime < FirstdayEnd
if (as.POSIXct(y) <= FirstdayEnd)
{
#Example fall into this case (2015-09-07 08:00-2015-09-07 18:32), just return diff of start/end time
outval<- as.numeric(difftime(as.POSIXct(y), as.POSIXct(x), units="hours"))
}
else
{
#Example fall into this case (2015-09-07 08:00-2015-09-07 20:11 ), return diff of start/ FirstdayEnd time
outval<- as.numeric(difftime(FirstdayEnd, as.POSIXct(x), units="hours"))
}
}
else #For case StartTime < FirstdayStart
{
#For case EndTime < FirstdayEnd
if (as.POSIXct(y) <= FirstdayEnd)
{
#Example fall into this case (2015-09-07 06:00-2015-09-07 18:32), just return diff of FirstdayStart/end time
outval<- as.numeric(difftime(as.POSIXct(y), FirstdayStart, units="hours"))
}
else
{
#Example fall into this case (2015-09-07 06:00-2015-09-07 20:11 ), return diff of FirstdayStart/ FirstdayEnd time
outval<- as.numeric(difftime(FirstdayEnd, FirstdayStart, units="hours"))
}
}
}
else # For case Start and End not in the same day , eg (2015-09-07 03:00", 2015-09-07 06:00)
{
# For case starttime < FirstdayStart Calculate the Firstday period first, 2015-09-03 02:00 2015-09-04 16:00
if (as.POSIXct(x)< FirstdayStart )
{
#Example fall into this case (2015-09-07 06:00 2015-09-08 20:11 ), firstdayPeriod return diff of FirstdayStart/ FirstdayEnd time
firstdayPeriod <- as.numeric(difftime(FirstdayEnd, FirstdayStart, units="hours"))
}
else
{
if (as.POSIXct(x) <= FirstdayEnd)
{
#Example fall into this case (2015-09-07 08:00 2015-09-08 20:11 ), firstdayPeriod = of Startime/ FirstdayEnd time
firstdayPeriod <- as.numeric(difftime(FirstdayEnd, as.POSIXct(x), units="hours"))
}
else
{ #Example fall into this case (2015-09-07 20:00 2015-09-08 20:11 ), firstdayPeriod=0
firstdayPeriod <- c(0)
}
}
#Calculate the last day period
if (as.POSIXct(y) > LastdayEnd)
{
#Example fall into this case (2015-09-07 08:00 2015-09-08 21:00 ), lastdayPeriod = of LastdayEnd/ LastdayStart time
lastdayPeriod <- as.numeric(difftime(FirstdayEnd, FirstdayStart, units="hours"))
}
else
{
if (as.POSIXct(y) >= LastdayStart)
{
#Example fall into this case (2015-09-07 08:00 2015-09-08 18:00 ), lastdayPeriod = of LastdayEnd/ endtime time
lastdayPeriod <- as.numeric(difftime(as.POSIXct(y),LastdayStart, units="hours"))
}
else
{
lastdayPeriod <- c(0)
}
}
#Calculate the overrall time
outval <- lastdayPeriod +firstdayPeriod + (numdays-1)* as.numeric(difftime(FirstdayEnd,FirstdayStart, units="hours"))
}
outval <- round(outval, digits=2)
if (outval < 0)
{
outval <- c(0)
}
outval
}
我得到如下结果
timedf$P0To7 <- apply(timedf[,c(2,3)], 1, function(x) getp1TOp2(x[1], x[2], 0, 7))
timedf$P7To19 <- apply(timedf[,c(2,3)], 1, function(x) getp1TOp2(x[1], x[2], 7, 19))
timedf$P19To24 <- apply(timedf[,c(2,3)], 1, function(x) getp1TOp2(x[1], x[2], 19, 24))
> timedf
spaceNum starttime endtime staytime P0To7 P7To19 P19To24
1 1 2015-09-03 00:00 2015-09-04 20:05 44.08 14.00 24.00 6.08
2 1 2015-09-04 23:18 2015-09-05 05:52 6.57 5.87 0.00 0.70
3 1 2015-09-05 05:59 2015-09-05 06:15 0.27 0.27 0.00 0.00
4 2 2015-09-03 06:19 2015-09-05 16:36 58.28 14.68 33.60 10.00
5 2 2015-09-06 09:03 2015-09-06 09:06 0.05 0.00 0.05 0.00
6 2 2015-09-06 09:10 2015-09-06 20:42 11.53 0.00 9.83 1.70