在R中显示年份的累计总和

时间:2018-06-12 23:02:48

标签: r

我的数据看起来像这样。

Month_Yr revenue year mo
2016-01    1200  2016 01
2016-02    7826  2016 02
2016-03   11892  2016 03
2016-05   11376  2016 05
2016-06    9055  2016 06
2016-07    5000  2016 07

我想创建一个包含每年累计收入总额的列,但是按月列出。所以它看起来像这样:

Month_Yr revenue year mo cumsum
2016-01    1200  2016 01 1200 
2016-02    7826  2016 02 9026
2016-03   11892  2016 03 20918
2016-05   11376  2016 05 32294
2016-06    9055  2016 06 41349
2016-07    5000  2016 07 46349

此数据延续至2018年,有些月份(例如2016年4月)没有任何价值,因此将其排除在外。谢谢!

2 个答案:

答案 0 :(得分:3)

library(tidyverse)
df%>%
separate(Month_Yr,c("year","month"),remove = F)%>%
group_by(year)%>%
mutate(cumsum=cumsum(Revenue))

在基地R你可以做到

transform(df,year=y<-sub("-.*","",Month_Yr),
          month=sub(".*-","",Month_Yr),revenue=ave(Revenue,y,FUN=cumsum))

答案 1 :(得分:1)

您可以尝试:

library(dplyr)
df <- data.frame("Month_Yr" = c("2016-01","2016-02","2016-03","2016-05","2016-06","2016-07","2017-01","2017-02","2017-03","2017-05","2017-06","2017-07","2018-01","2018-02","2018-03","2018-05","2018-06","2018-07"), "Revenue" = c(1200,7826,11892,11376,9055,5000))
df$year <- substr(df$Month_Yr,0,4)
df$mo <- substr(df$Month_Yr,6,7)

df <- df %>%
  arrange(year,mo) %>%
  group_by(year) %>%
  mutate(cumsum = cumsum(Revenue))

更新了答案。