1

在下面的示例 xts 数据中,我展示了一小部分日内数据。

my.temp <- structure(c(1134.65, 45665.2, 63639.8, 262817.8, 71898.4, 128737,
  641741.8, 6090, 7561.5, 3954.73, 15733.2, 274.88, 824.64, 1099.52, 1924.16,
  348715, 196425, 113975, 215825, 38340, 21138, 12020, 20200, 20200, 20200,
  20200, 20200, 20200, 23000, 12020, 12020, 12020, 12020, 12020, 12020, 12020,
  20200, 20200, 20200, 20200, 12020, 20000), .Dim = c(21L, 2L),
  .Dimnames = list(NULL, c("VALUE", "USAP")), index = structure(c(1378130401,
  1378130404, 1378130404, 1378130404, 1378130404, 1378130404, 1378130404,
  1378130404, 1378130406, 1378130409, 1378130411, 1378130415, 1378130415,
  1378130415, 1378130415, 1378130451, 1378130452, 1378130452, 1378130452,
  1378130455, 1378130501), tzone = "", tclass = c("POSIXct", "POSIXt")),
  .indexCLASS = c("POSIXct", "POSIXt"), tclass = c("POSIXct", "POSIXt"),
  .indexTZ = "", tzone = "", class = c("xts", "zoo"))

我试图显示每个 USAP 的总价值。aggregate如果我想在整个期间显示结果,我已经成功使用了该功能。如何显示每天每个 USAP 的总数?

4

2 回答 2

0

这是一种方法。可能有更简洁的方法,但这是我目前能想到的最好的方法。

# first, split your object into groups by USAP
temp.split <- split(my.temp, my.temp$USAP)
# define a simple helper function
f <- function(x) apply.daily(x, function(d) c(sum(d$VALUE),d$USAP[1]))
# then call apply.daily on each group;
# finally, rbind the results back together
do.call(rbind, lapply(temp.split, f))
#                          VALUE  USAP
# 2013-09-02 09:00:04    6090.00 23000
# 2013-09-02 09:00:52 2089440.00 20200
# 2013-09-02 09:00:55   70847.28 12020
# 2013-09-02 09:01:41   21138.00 20000
于 2013-09-03T13:37:25.187 回答
0
aggregate(VALUE ~ USAP + as.Date(index(my.temp)), data = my.temp, sum)

   #    USAP as.Date(index(my.temp))      VALUE
   # 1 12020              2013-09-02   70847.28
   # 2 20000              2013-09-02   21138.00
   # 3 20200              2013-09-02 2089440.00
   # 4 23000              2013-09-02    6090.00
于 2013-09-03T14:08:46.657 回答