如何计算每个月和每年的总数?
How to compute total number per each month and year?
嘿,我有一个包含 2 列的数据框:日期和案例:
date cases
2021-05-01 2
2022-03-01 3
2021-05-21 8
我想要的是计算每年每个月每个案例的总数。
我找到了代码:
total_cases <- aggregate(cbind(cases)~month(date),
data=datalab,FUN=sum)
但不是按年分开的。它计算每个月的总病例数,但将 2021 年 5 月和 2022 年 5 月合并在一起,我想要分开计算。
使用months
(注意s),以及strftime
.
with(datalab, aggregate(datalab['cases'], list(month=months(date), year=strftime(date, '%Y')), FUN=sum))
# month year cases
# 1 May 2021 10
# 2 March 2022 3
您也可以使用第 1st 到 7th substr
ing.
aggregate(datalab['cases'], list(month=substr(datalab$date, 1, 7)), FUN=sum)
# month cases
# 1 2021-05 10
# 2 2022-03 3
或者,更简洁,虽然不是很好。
aggregate(cases ~ substr(date, 1, 7), data=datalab, FUN=sum)
# substr(date, 1, 7) cases
# 1 2021-05 10
# 2 2022-03 3
更新
要同时按国家汇总,只需将其放在第二个(即 by=
)列表中即可;它被 with
.
引用
with(datalab2, aggregate(datalab2['cases'], list(month=months(date), year=strftime(date, '%Y'),
country=country), FUN=sum))
# month year country cases
# 1 May 2021 A 15
# 2 March 2022 A 5
# 3 May 2021 B 16
# 4 March 2022 B 6
# 5 May 2021 C 12
# 6 March 2022 C 5
数据:
datalab <- structure(list(date = c("2021-05-01", "2022-03-01", "2021-05-21"
), cases = c(2L, 3L, 8L)), class = "data.frame", row.names = c(NA,
-3L))
datalab <- transform(datalab, date=as.Date(date))
datalab2 <- structure(list(date = structure(c(18748, 19052, 18768, 18748,
19052, 18768, 18748, 19052, 18768), class = "Date"), country = structure(c(1L,
1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L), levels = c("A", "B", "C"), class = "factor"),
cases = c(7L, 5L, 8L, 9L, 6L, 7L, 2L, 5L, 10L)), class = "data.frame", row.names = c(NA,
-9L))
嘿,我有一个包含 2 列的数据框:日期和案例:
date cases
2021-05-01 2
2022-03-01 3
2021-05-21 8
我想要的是计算每年每个月每个案例的总数。 我找到了代码:
total_cases <- aggregate(cbind(cases)~month(date),
data=datalab,FUN=sum)
但不是按年分开的。它计算每个月的总病例数,但将 2021 年 5 月和 2022 年 5 月合并在一起,我想要分开计算。
使用months
(注意s),以及strftime
.
with(datalab, aggregate(datalab['cases'], list(month=months(date), year=strftime(date, '%Y')), FUN=sum))
# month year cases
# 1 May 2021 10
# 2 March 2022 3
您也可以使用第 1st 到 7th substr
ing.
aggregate(datalab['cases'], list(month=substr(datalab$date, 1, 7)), FUN=sum)
# month cases
# 1 2021-05 10
# 2 2022-03 3
或者,更简洁,虽然不是很好。
aggregate(cases ~ substr(date, 1, 7), data=datalab, FUN=sum)
# substr(date, 1, 7) cases
# 1 2021-05 10
# 2 2022-03 3
更新
要同时按国家汇总,只需将其放在第二个(即 by=
)列表中即可;它被 with
.
with(datalab2, aggregate(datalab2['cases'], list(month=months(date), year=strftime(date, '%Y'),
country=country), FUN=sum))
# month year country cases
# 1 May 2021 A 15
# 2 March 2022 A 5
# 3 May 2021 B 16
# 4 March 2022 B 6
# 5 May 2021 C 12
# 6 March 2022 C 5
数据:
datalab <- structure(list(date = c("2021-05-01", "2022-03-01", "2021-05-21"
), cases = c(2L, 3L, 8L)), class = "data.frame", row.names = c(NA,
-3L))
datalab <- transform(datalab, date=as.Date(date))
datalab2 <- structure(list(date = structure(c(18748, 19052, 18768, 18748,
19052, 18768, 18748, 19052, 18768), class = "Date"), country = structure(c(1L,
1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L), levels = c("A", "B", "C"), class = "factor"),
cases = c(7L, 5L, 8L, 9L, 6L, 7L, 2L, 5L, 10L)), class = "data.frame", row.names = c(NA,
-9L))