基于R中时间列中的秒值的和列值

时间:2017-12-10 04:45:17

标签: r datetime data.table xts

我有兴趣了解如何根据data.table中时间列的秒值对列进行求和。

例如,我们假设我有一个数据表如下:

Time                   |      Inventory
----------------------------------------------     
08-01-2001 11:50:12    |       5
08-01-2001 11:50:16    |       8
08-01-2001 11:50:17    |       2
08-01-2001 11:50:17    |       1
08-01-2001 11:50:19    |       10
08-01-2001 11:50:23    |       5
08-01-2001 11:50:23    |       9
08-01-2001 11:51:23    |       12

然后,我希望能够根据Inventory列中的秒值对Time求和,以便得到data.table,如下所示:

Time                   |      Inventory
----------------------------------------------     
08-01-2001 11:50:12    |       5
08-01-2001 11:50:16    |       8
08-01-2001 11:50:17    |       3
08-01-2001 11:50:17    |       3
08-01-2001 11:50:19    |       10
08-01-2001 11:50:23    |       14
08-01-2001 11:50:23    |       14
08-01-2001 11:51:23    |       12

我尝试使用Aggregate()函数的变体,但这些似乎总是删除重复的行,我不希望这样做。有没有办法可以使用data.tablexts来执行此操作?提前谢谢。

编辑:这是dput输出:

structure(list(Timecol = c("0008-01-20 00:00:00", "0008-01-20 00:00:00", "0008-01-20 00:00:00", "0008-01-20 00:00:00", "0008-01-20 00:00:00", "0008-01-20 00:00:00", "0008-01-20 00:00:00", "0008-01-20 00:00:00" ), ID = c("11", "11", "11", "11", "11", "11", "11", "11"), Inventorycol = c("5", "8", "2", "1", "10", "5", "9", "12")), .Names = c("Timecol", "ID", "Inventorycol"), row.names = c(NA, -8L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x00000000028b0788>)

3 个答案:

答案 0 :(得分:1)

您可以使用ave功能:

 (dat$Sum=ave(dat$Inventory,dat$Time,FUN=sum))
                      Time Inventory Sum
 1 08-01-2001 11:50:12             5   5
 2 08-01-2001 11:50:16             8   8
 3 08-01-2001 11:50:17             2   3
 4 08-01-2001 11:50:17             1   3
 5 08-01-2001 11:50:19            10  10
 6 08-01-2001 11:50:23             5  14
 7 08-01-2001 11:50:23             9  14

使用的数据:

 dat=read.table(text="
 Time                   |      Inventory
 08-01-2001 11:50:12    |       5
 08-01-2001 11:50:16    |       8
 08-01-2001 11:50:17    |       2
 08-01-2001 11:50:17    |       1
 08-01-2001 11:50:19    |       10
 08-01-2001 11:50:23    |       5
 08-01-2001 11:50:23    |       9",sep="|",h=T,stringsAsFactors=F)

答案 1 :(得分:1)

您可以使用

dat[, Sum := sum(Inventory), by = Time]
#                   Time Inventory Sum
# 1: 08-01-2001 11:50:12         5   5
# 2: 08-01-2001 11:50:16         8   8
# 3: 08-01-2001 11:50:17         2   3
# 4: 08-01-2001 11:50:17         1   3
# 5: 08-01-2001 11:50:19        10  10
# 6: 08-01-2001 11:50:23         5  14
# 7: 08-01-2001 11:50:23         9  14

答案 2 :(得分:0)

以下是xts

的方法
# create xts object:

dat=read.table(text="
 Time                   |      Inventory
 08-01-2001 11:50:12    |       5
 08-01-2001 11:50:16    |       8
 08-01-2001 11:50:17    |       2
 08-01-2001 11:50:17    |       1
 08-01-2001 11:50:19    |       10
 08-01-2001 11:50:23    |       5
 08-01-2001 11:50:23    |       9",sep="|",h=T,stringsAsFactors=F)

dat[, "Time2"] <- as.POSIXct(dat[, "Time"], format = "%d-%m-%Y %H:%M:%S")
x <- xts(dat[, "Inventory"], order.by = dat[, "Time2"])

sum_with_inv <- function(y) {
  Sum <- sum(y)
  m <- merge(y, Sum)
  m
}



y <- do.call(rbind, lapply(split(x, f = "seconds"), sum_with_inv))
y
#                      y Sum
# 2001-01-08 11:50:12  5   5
# 2001-01-08 11:50:16  8   8
# 2001-01-08 11:50:17  2   3
# 2001-01-08 11:50:17  1   3
# 2001-01-08 11:50:19 10  10
# 2001-01-08 11:50:23  5  14
# 2001-01-08 11:50:23  9  14