我试图分别获取每年一个月中所有值的总和 ,这意味着该月的每一天都将总和汇总为每月汇总,即包含以下数据>
Date Value
<dttm> <dbl>
1 2016-07-04 00:00:00 -9
2 2018-10-05 00:00:00 31.3
3 2018-04-10 00:00:00 -3
4 2016-09-02 00:00:00 -56
5 2017-01-02 00:00:00 -6
6 2018-02-01 00:00:00 -39
7 2015-07-24 00:00:00 -14.5
8 2017-06-08 00:00:00 -16
9 2017-01-04 00:00:00 500
10 2015-11-03 00:00:00 -2.46
我想从中得到一些东西(不是实际值):
Date Value
<dttm> <dbl>
1 August - 2017 -9
2 September - 2017 31.3
3 October - 2017 -3
我尝试运行以下命令
group_by(td %>%
mutate(year = lubridate::year(Date)),
year) %>%
aggregate(.$Value,
by = list(date = lubridate::month(.$Date)),
FUN = sum)
没有运气,编译器会发回此错误:
Error in Summary.POSIXct(c(1517184000, 1517184000, 1517184000, 1516924800, :
'sum' non défini pour des objets "POSIXt"
我还尝试了这些线程的变体:1,2和the one from the third message;每个都没有给出我期望的结果。 有解决方案的想法吗?
dput()格式的数据。
td <-
structure(list(Date = structure(c(1467590400, 1538697600, 1523318400,
1472774400, 1483315200, 1517443200, 1437696000, 1496880000, 1483488000,
1446508800), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
Value = c(-9, 31.3, -3, -56, -6, -39, -14.5, -16, 500, -2.46
)), row.names = c("1", "2", "3", "4", "5", "6", "7", "8",
"9", "10"), class = "data.frame")