[英]Summing column by time period in R
我想根據時間段(日期)總結成本
數據如 -
Date | Cost
1-1-22 | $5
1/1/22 | $10
1/1/22 | $8
2/5-22 | $9
2/5/22 | $10
3/5/22 | $5
3/7/22 | $10
...
12/12/22| $X
我將如何添加單個月、季度和年度的總成本?
以下是您可以執行的步驟:
lubridate::dmy()
將您的日期列轉換為實際日期(假設您的日期采用日/月/年格式)。group_by
並從dplyr
package 中summarise
來計算這些時間段的總數。library('dplyr')
library('lubridate')
tempDF <- data.frame(
Date = c('1-1-22', '1/1/22', '1/1/22', '2/5-22', '2/5/22', '3/5/22', '3/7/22'),
Cost = c(5, 10, 8, 9, 10, 5, 10)
)
# there's probably a way to do this within the statement below, but I can't think of a neat way
quarters <- data.frame(Month=1:12, Quarter=paste0('Q', rep(1:4, each=3)))
tempDF <- tempDF %>%
mutate(
Date = lubridate::dmy(Date),
Month = lubridate::month(Date),
Year = lubridate::year(Date)
) %>%
left_join(quarters, by='Month')
tempDF %>%
group_by(Year) %>%
summarise(total = sum(Cost))
# A tibble: 1 × 2
# Year total
# <dbl> <dbl>
# 1 2022 57
tempDF %>%
group_by(Quarter) %>%
summarise(total = sum(Cost))
# A tibble: 3 × 2
# Quarter total
# <chr> <dbl>
# 1 Q1 23
# 2 Q2 24
# 3 Q3 10
tempDF %>%
group_by(Month) %>%
summarise(total = sum(Cost))
# A tibble: 3 × 2
# Month total
# <dbl> <dbl>
# 1 1 23
# 2 5 24
# 3 7 10
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.