你好早上好
我正在尝试编写使用自定义12个月窗口的R代码,并将其与默认的12个月窗口相对于一年。例如,lubridate软件包将(2017年1月1日)视为(2017年12月31日),我可以使用DPLYR获得该年的总数软件包,但我想将(2017年6月1日)设置为(2018年6月1日),并将其作为自定义年份的总计。
这是我的R代码,我已经尽力使它对堆栈溢出友好,但是我仍然是新手,请告诉我是否还有其他需要。
library(dplyr)
library(lubridate)
library(ggplot2) # this will be used for graphs later on
library(ISLR) # this is for example datasets, but I wont be using it
#creating year-month-day vector called year_s, I am not calling it years
#because lubridate package contains years as a keyword
year_s <- as.Date(c('2014-01-01','2014-06-01','2014-12-31','2015-01-01','2015-06-01','2015-12-31','2016-01-01','2016-06-01','2016-12-31','2017-06-01'))
#hypothetical sales numbers for the year vector
sales <- c(2500,8500,1500,7500,9573,64,5800,6300,4570,10050)
#creating a dataframe for year_s, and sales
yearly_sales <- data.frame(year_s,sales)
#making sure that years_s column is set to ymd, using ymd from lubridate
yearly_sales$year_s <- ymd(yearly_sales$year_s)
year_s sales
<date> <dbl>
1 2014-01-01 2500
2 2014-06-01 8500
3 2014-12-31 1500
4 2015-01-01 7500
5 2015-06-01 9573
6 2015-12-31 64
7 2016-01-01 5800
8 2016-06-01 6300
9 2016-12-31 4570
10 2017-06-01 10050
#converting to years from lubridate (my issue is here, because instead of
#calendar year, I want to define a custom calendar year, I will go over this
#again later on)
yearly_sales$year_s <- year(yearly_sales$year_s)
#using dplyr to total the sums for each calendar year
yearly_sales %>% group_by(year_s) %>% summarise_all(funs(sum))
year_s sales
<dbl> <dbl>
1 2014 12500
2 2015 17137
3 2016 16670
4 2017 10050
#the issue is that with this code I get the sums for calendar year, but I
#would like to define a custom calendat year (e.g as treating 2014 -01-01,
#2014-06-01, and 2014-12-31 as a year, I would like 2016-06-01, 2016-12-
#31,2017-06-01 as an year)
#[Picture for Calendar years and the result I am looking for fiscal years][1]
https://i.stack.imgur.com/KpVLg.png