1

我有一个包含超过一百万行数据的数据框(代理和按天汇总的呼叫指标)。每个代理都被多次列出,因为他们每天处理多个队列中的呼叫 (d1$Calls)。我想确定代理人在现场工作的周数。我通常可以使用“difftime”来获得任何给定日期的代理开始日期(d1$Start)和交互日期(d1$Interaction)之间的差异:

floor(difftime(d1$Interaction,d1$Start,units='weeks')) 

但是,我的系统的开始日期不可靠,通常会导致负数周数:

dput(d1)
structure(list(ID = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L, 2L), .Label = c("a123", "b123"), class = "factor"), QUEUE = structure(c(9L, 
8L, 7L, 6L, 5L, 3L, 4L, 1L, 2L, 4L), .Label = c("MHEK", "MMED", 
"MMEF", "MMEM", "MNEM", "MSED", "MSEE", "MSEK", "MSEP"), class = "factor"), 
Calls = c(1L, 4L, 25L, 14L, 6L, 25L, 5L, 1L, 1L, 3L), Interaction = structure(list(
sec = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), min = c(0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), hour = c(0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L), mday = c(2L, 2L, 6L, 12L, 
12L, 2L, 6L, 6L, 6L, 6L), mon = c(0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L), year = c(115L, 115L, 115L, 115L, 
115L, 115L, 115L, 115L, 115L, 115L), wday = c(5L, 5L, 
2L, 1L, 1L, 5L, 2L, 2L, 2L, 2L), yday = c(1L, 1L, 5L, 
11L, 11L, 1L, 5L, 5L, 5L, 5L), isdst = c(0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L), zone = c("PST", "PST", "PST", 
"PST", "PST", "PST", "PST", "PST", "PST", "PST"), gmtoff = c(NA_integer_, 
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
NA_integer_, NA_integer_, NA_integer_, NA_integer_)), .Names = c("sec", 
"min", "hour", "mday", "mon", "year", "wday", "yday", "isdst", 
"zone", "gmtoff"), class = c("POSIXlt", "POSIXt")), Start = structure(list(
sec = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), min = c(0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), hour = c(0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L), mday = c(1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L), mon = c(2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L), year = c(115L, 115L, 115L, 115L, 
115L, 115L, 115L, 115L, 115L, 115L), wday = c(0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), yday = c(59L, 59L, 59L, 
59L, 59L, 59L, 59L, 59L, 59L, 59L), isdst = c(0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), zone = c("PST", "PST", 
"PST", "PST", "PST", "PST", "PST", "PST", "PST", "PST"
), gmtoff = c(NA_integer_, NA_integer_, NA_integer_, 
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
NA_integer_, NA_integer_)), .Names = c("sec", "min", 
"hour", "mday", "mon", "year", "wday", "yday", "isdst", "zone", 
"gmtoff"), class = c("POSIXlt", "POSIXt")), Weeks = structure(c(-9, 
-9, -8, -7, -7, -9, -8, -8, -8, -8), units = "weeks", class = "difftime")), .Names = c("ID", 
"QUEUE", "Calls", "Interaction", "Start", "Weeks"), row.names = c(NA, 
-10L), class = "data.frame")

为了解决这个问题,我想计算任何交互日期 (d1$Interaction) 与该代理在系统中的第一次交互日期 (d1$ID) 之间的周差。这怎么可能?

4

1 回答 1

1

这对我有用(全部在基础 R 中):

#split the data frame according to ID
mylist <- split(df, factor(df$ID))

#use do.call to combine lists elements to one data.frame
#instead of do call you can use data.table::rbindlist for speed
mydata <- do.call(rbind,
lapply(mylist, function(x) {
               #order each group
               x <- x[order(x$Interaction),]
               #calculate time differences
               #difftime of Interactions vector from the 2nd element to the last, minus
               #the Interactions vector of the 1st element to the penultimate
               #I use c(0, difftime.... to add a zero to the first difference
               #so that I can add it as a column
               x$weekdif <- c(0,difftime(x$Interaction[2:length(x$Interaction)],  
                            x$Interaction[1:(length(x$Interaction)-1)],
                            units='weeks'))
               x
}))

输出:

> mydata
          ID QUEUE Calls Interaction      Start    Weeks   weekdif
a123.1  a123  MSEP     1  2015-01-02 2015-03-01 -9 weeks 0.0000000
a123.2  a123  MSEK     4  2015-01-02 2015-03-01 -9 weeks 0.0000000
a123.3  a123  MSEE    25  2015-01-06 2015-03-01 -8 weeks 0.5714286
a123.4  a123  MSED    14  2015-01-12 2015-03-01 -7 weeks 0.8571429
a123.5  a123  MNEM     6  2015-01-12 2015-03-01 -7 weeks 0.0000000
b123.6  b123  MMEF    25  2015-01-02 2015-03-01 -9 weeks 0.0000000
b123.7  b123  MMEM     5  2015-01-06 2015-03-01 -8 weeks 0.5714286
b123.8  b123  MHEK     1  2015-01-06 2015-03-01 -8 weeks 0.0000000
b123.9  b123  MMED     1  2015-01-06 2015-03-01 -8 weeks 0.0000000
b123.10 b123  MMEM     3  2015-01-06 2015-03-01 -8 weeks 0.0000000

我将功能更改为以下内容,现在它可以按您的意愿工作:

#you need to import this for the na.locf function
library(zoo)

mylist <- split(df, factor(df$ID))
mydata <- do.call(rbind,
lapply(mylist, function(x) {
  x <- x[order(x$Interaction),]
  x$weekdif <- c(0,difftime(x$Interaction[2:length(x$Interaction)], x$Interaction[1:(length(x$Interaction)-1)], units='weeks'))
  #convert all zeros (apart from first to NAs)
  x$weekdif[x$weekdif==0] <- NA 
  #create the rolling values minus the first NAs
  #see the examples at ?na.locf for details on what it does
  temp <- as.numeric(na.locf(zoo(x$weekdif)))
  #add the first NAs
  missing_length <- length(x$weekdif) - length(temp)
  x$weekdif <- c(rep(0,missing_length), temp)
  x
}))

输出:

          ID QUEUE Calls Interaction      Start    Weeks   weekdif
a123.1  a123  MSEP     1  2015-01-02 2015-03-01 -9 weeks 0.0000000
a123.2  a123  MSEK     4  2015-01-02 2015-03-01 -9 weeks 0.0000000
a123.3  a123  MSEE    25  2015-01-06 2015-03-01 -8 weeks 0.5714286
a123.4  a123  MSED    14  2015-01-12 2015-03-01 -7 weeks 0.8571429
a123.5  a123  MNEM     6  2015-01-12 2015-03-01 -7 weeks 0.8571429
b123.6  b123  MMEF    25  2015-01-02 2015-03-01 -9 weeks 0.0000000
b123.7  b123  MMEM     5  2015-01-06 2015-03-01 -8 weeks 0.5714286
b123.8  b123  MHEK     1  2015-01-06 2015-03-01 -8 weeks 0.5714286
b123.9  b123  MMED     1  2015-01-06 2015-03-01 -8 weeks 0.5714286
b123.10 b123  MMEM     3  2015-01-06 2015-03-01 -8 weeks 0.5714286

每个 id 的第一个值为 0,因为没有以前的交互日期。

于 2015-06-30T22:52:22.980 回答