我想在电话系统上随时测量同时呼叫的数量。与此SQL example相同,但使用R和CSV文件。
对于具有Id(数字),TimeOfCall(POSIXct),TimeOfClose(POSIXct)作为3列类型的CSV文件,我将执行以下操作,但我想知道是否有更有效的方法在R中执行此操作一台Windows机器?)
#Small dataset example. Much larger in reality
id <- c(1,2,3,4)
TimeOfStart <- c(as.POSIXct("2013-01-01 10:10:00", format="%Y-%d-%m %H:%M:%S"), as.POSIXct("2013-01-01 10:14:00", format="%Y-%d-%m %H:%M:%S"), as.POSIXct("2013-03-01 10:10:00", format="%Y-%d-%m %H:%M:%S"), as.POSIXct("2013-03-01 10:20:00", format="%Y-%d-%m %H:%M:%S"))
TimeOfEnd <- c(as.POSIXct("2013-01-01 10:20:00", format="%Y-%d-%m %H:%M:%S"), as.POSIXct("2013-01-01 10:44:00", format="%Y-%d-%m %H:%M:%S"), as.POSIXct("2013-03-01 10:21:00", format="%Y-%d-%m %H:%M:%S"), as.POSIXct("2013-03-01 10:25:00", format="%Y-%d-%m %H:%M:%S"))
call_data <- data.frame(id, TimeOfStart, TimeOfEnd)
#holder for all POSIX converted to numeric entries
stringoftimes <- '0'
# loop through all rows and then concatenate all entries between start and end time after converting POSIX to numberics.
for (i in 1:nrow(call_data))
{
stringoftimes <- c(stringoftimes,c(as.numeric(call_data$TimeOfStart[i]):as.numeric(call_data$TimeOfStop[i])))
}
#Convert to table so that count of entries takes place
stringoftimes <- as.data.frame(table(stringoftimes))
#Sort table to see highest results first
stringoftimes <- stringoftimes[order(stringoftimes$Freq, decreasing=TRUE),]
答案 0 :(得分:0)
Howsabout然后:
计算此时间点序列的呼叫数,以分钟为单位:
> t = seq(min(call_data$TimeOfStart),max(call_data$TimeOfEnd),by=60)
功能是这样的:
> ncalls = Vectorize(function(t){sum(t>=call_data$TimeOfStart & t<=call_data$TimeOfEnd)})
绘制为阶梯函数:
> plot(t,ncalls(t),type="s")