I'm trying to generate a vector for an api import package which accepts the following arguments
date.from
Start date in YYYY-MM-DD.
date.to
End date in YYYY-MM-DD.
Since the date range is quite big and I'm trying to get the data without sampling, I have to split the request into smaller ones, otherwise I get error 400 and an advice to try smaller date range.
My code looks like this:
library(lubridate)
today <- Sys.Date()
start_date <- seq(as.Date("2013-01-01"), as.Date(today), by = "months")
end_date <- seq(as.Date("2013-02-01"), as.Date(today), by = "months")-1
It results in 2 sets of dates for start and end dates each, however the set with start date has 1st day of the current month, but end date has nothing because current month isn't complete (today is 2020-09-12)
> start_date
[1] "2013-01-01" "2013-02-01" "2013-03-01" "2013-04-01" "2013-05-01" "2013-06-01" "2013-07-01"
[8] "2013-08-01" "2013-09-01" "2013-10-01" "2013-11-01" "2013-12-01" "2014-01-01" "2014-02-01"
[15] "2014-03-01" "2014-04-01" "2014-05-01" "2014-06-01" "2014-07-01" "2014-08-01" "2014-09-01"
[22] "2014-10-01" "2014-11-01" "2014-12-01" "2015-01-01" "2015-02-01" "2015-03-01" "2015-04-01"
[29] "2015-05-01" "2015-06-01" "2015-07-01" "2015-08-01" "2015-09-01" "2015-10-01" "2015-11-01"
[36] "2015-12-01" "2016-01-01" "2016-02-01" "2016-03-01" "2016-04-01" "2016-05-01" "2016-06-01"
[43] "2016-07-01" "2016-08-01" "2016-09-01" "2016-10-01" "2016-11-01" "2016-12-01" "2017-01-01"
[50] "2017-02-01" "2017-03-01" "2017-04-01" "2017-05-01" "2017-06-01" "2017-07-01" "2017-08-01"
[57] "2017-09-01" "2017-10-01" "2017-11-01" "2017-12-01" "2018-01-01" "2018-02-01" "2018-03-01"
[64] "2018-04-01" "2018-05-01" "2018-06-01" "2018-07-01" "2018-08-01" "2018-09-01" "2018-10-01"
[71] "2018-11-01" "2018-12-01" "2019-01-01" "2019-02-01" "2019-03-01" "2019-04-01" "2019-05-01"
[78] "2019-06-01" "2019-07-01" "2019-08-01" "2019-09-01" "2019-10-01" "2019-11-01" "2019-12-01"
[85] "2020-01-01" "2020-02-01" "2020-03-01" "2020-04-01" "2020-05-01" "2020-06-01" "2020-07-01"
[92] "2020-08-01" "2020-09-01"
> end_date
[1] "2013-01-30" "2013-03-02" "2013-03-30" "2013-04-30" "2013-05-30" "2013-06-30" "2013-07-30"
[8] "2013-08-30" "2013-09-30" "2013-10-30" "2013-11-30" "2013-12-30" "2014-01-30" "2014-03-02"
[15] "2014-03-30" "2014-04-30" "2014-05-30" "2014-06-30" "2014-07-30" "2014-08-30" "2014-09-30"
[22] "2014-10-30" "2014-11-30" "2014-12-30" "2015-01-30" "2015-03-02" "2015-03-30" "2015-04-30"
[29] "2015-05-30" "2015-06-30" "2015-07-30" "2015-08-30" "2015-09-30" "2015-10-30" "2015-11-30"
[36] "2015-12-30" "2016-01-30" "2016-03-01" "2016-03-30" "2016-04-30" "2016-05-30" "2016-06-30"
[43] "2016-07-30" "2016-08-30" "2016-09-30" "2016-10-30" "2016-11-30" "2016-12-30" "2017-01-30"
[50] "2017-03-02" "2017-03-30" "2017-04-30" "2017-05-30" "2017-06-30" "2017-07-30" "2017-08-30"
[57] "2017-09-30" "2017-10-30" "2017-11-30" "2017-12-30" "2018-01-30" "2018-03-02" "2018-03-30"
[64] "2018-04-30" "2018-05-30" "2018-06-30" "2018-07-30" "2018-08-30" "2018-09-30" "2018-10-30"
[71] "2018-11-30" "2018-12-30" "2019-01-30" "2019-03-02" "2019-03-30" "2019-04-30" "2019-05-30"
[78] "2019-06-30" "2019-07-30" "2019-08-30" "2019-09-30" "2019-10-30" "2019-11-30" "2019-12-30"
[85] "2020-01-30" "2020-03-01" "2020-03-30" "2020-04-30" "2020-05-30" "2020-06-30" "2020-07-30"
[92] "2020-08-30"
Which I'm later feeding through the loop like this:
metrika_sources <- list()
for (i in 1:length(start_date)) {
for (j in 1:length(end_date)) {
rym_sources <-
rym_get_data(
counters = "******",
date.from = start_date[i],
date.to = end_date[j],
dimensions = "ym:s:date,ym:s:lastTrafficSource,ym:s:lastSourceEngine,ym:s:deviceCategory,ym:s:UTMSource,ym:s:UTMMedium,ym:s:UTMCampaign",
metrics = "ym:s:visits,ym:s:pageviews,ym:s:users,ym:s:bounceRate,ym:s:pageDepth,ym:s:avgVisitDurationSeconds,ym:s:productPurchasedQuantity",
sort = "-ym:s:date",
login = "******",
token.path = "*****",
accuracy = "full",
)
}
}
Is there a way to make the sequence finish with the today's or yesterday's date?
Thank you for helping out