我想在电话系统上随时测量同时呼叫的数量。与SQL example相同,但使用R和CSV文件。R中的同时呼叫计数
对于具有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),]
示例不起作用,因为call_data $ TimeOfCall/End应该是TimeOfStart/Stop? – Spacedman 2014-09-23 14:40:47
谢谢Spacedman,更新错字 – 2014-09-23 15:02:52