Good afternoon. I am attempting to make a sequence of month-day dates. The desired result would be October 1st through December 31st. I think the code is simple enough to show my intent, but is flawed in its logic. Thanks very much for any help here.
start <- as.Date("10/1", "m%-d%")
end <- as.Date("12/31", "m%-d%")
seq(start, end, by = "1 Day")
We can create a date sequence (any year would work, no need to be 2001 in my example), and then use the format function.
format(seq.Date(as.Date("2001-10-31"), as.Date("2001-12-31"), by = 1),
format = "%m-%d")
# [1] "10-31" "11-01" "11-02" "11-03" "11-04" "11-05" "11-06" "11-07" "11-08" "11-09" "11-10"
# [12] "11-11" "11-12" "11-13" "11-14" "11-15" "11-16" "11-17" "11-18" "11-19" "11-20" "11-21"
# [23] "11-22" "11-23" "11-24" "11-25" "11-26" "11-27" "11-28" "11-29" "11-30" "12-01" "12-02"
# [34] "12-03" "12-04" "12-05" "12-06" "12-07" "12-08" "12-09" "12-10" "12-11" "12-12" "12-13"
# [45] "12-14" "12-15" "12-16" "12-17" "12-18" "12-19" "12-20" "12-21" "12-22" "12-23" "12-24"
# [56] "12-25" "12-26" "12-27" "12-28" "12-29" "12-30" "12-31"
Alternatively with lubridate:
library(lubridate)
format( seq(ymd('2018-10-01'),ymd('2018-12-31'),by='days'), "%m-%d" )
[1] "10-01" "10-02" "10-03" "10-04" "10-05" "10-06" "10-07" "10-08" "10-09"
...etc to "12-31"
Related
I want to generate a sequence of dates with one quarter interval, with a starting date and ending date. I have below code :
> seq(as.Date('1980-12-31'), as.Date('1985-06-30'), by = 'quarter')
[1] "1980-12-31" "1981-03-31" "1981-07-01" "1981-10-01" "1981-12-31"
[6] "1982-03-31" "1982-07-01" "1982-10-01" "1982-12-31" "1983-03-31"
[11] "1983-07-01" "1983-10-01" "1983-12-31" "1984-03-31" "1984-07-01"
[16] "1984-10-01" "1984-12-31" "1985-03-31"
As you can see, this is not generating right sequence, as I dont understand how the date "1981-07-01" is coming here, I would expect "1981-06-30".
Is there any way to generate such sequence correctly with quarter interval?
Thanks for your time.
The from and to dates in the question are both end-of-quarter dates so we assume that that is the general case you are interested in.
1) Create a sequence of yearqtr objects yq and then convert them to Date class. frac=1 tells it s to use the end of the month. Alternately just use yq since that directly models years with quarters.
library(zoo)
from <- as.Date('1980-12-31')
to <- as.Date('1985-06-30')
yq <- seq(as.yearqtr(from), as.yearqtr(to), by = 1/4)
as.Date(yq, frac = 1)
giving;
[1] "1980-12-31" "1981-03-31" "1981-06-30" "1981-09-30" "1981-12-31"
[6] "1982-03-31" "1982-06-30" "1982-09-30" "1982-12-31" "1983-03-31"
[11] "1983-06-30" "1983-09-30" "1983-12-31" "1984-03-31" "1984-06-30"
[16] "1984-09-30" "1984-12-31" "1985-03-31" "1985-06-30"
2) or without any packages add 1 to from and to so that they are at the beginning of the next month, create the sequence (it has no trouble with first of month sequences) and then subtract 1 from the generated sequence giving the same result as above.
seq(from + 1, to + 1, by = "quarter") - 1
Using the clock package and R >= 4.1:
library(clock)
seq(year_quarter_day(1980, 4), year_quarter_day(1985, 2), by = 1) |>
set_day("last") |>
as_date()
# [1] "1980-12-31" "1981-03-31" "1981-06-30" "1981-09-30" "1981-12-31" "1982-03-31" "1982-06-30" "1982-09-30" "1982-12-31"
# [10] "1983-03-31" "1983-06-30" "1983-09-30" "1983-12-31" "1984-03-31" "1984-06-30" "1984-09-30" "1984-12-31" "1985-03-31"
# [19] "1985-06-30"
Note that this includes the final quarter. I don't know if that was your intent.
Different definition of "quarter". A quarter might well be (although it is not in R) 365/4 days. Look at output of :
as.Date('1980-12-31')+(365/4)*(0:12)
#[1] "1980-12-31" "1981-04-01" "1981-07-01" "1981-09-30" "1981-12-31" "1982-04-01" "1982-07-01" "1982-09-30"
#[9] "1982-12-31" "1983-04-01" "1983-07-01" "1983-09-30" "1983-12-31"
In order to avoid the days of the month from surprising you, you need to use a starting day of the month between 1 and 28, at least in non-leap years.
seq(as.Date('1981-01-01'), as.Date('1985-06-30'), by = 'quarter')
[1] "1981-01-01" "1981-04-01" "1981-07-01" "1981-10-01" "1982-01-01" "1982-04-01" "1982-07-01" "1982-10-01"
[9] "1983-01-01" "1983-04-01" "1983-07-01" "1983-10-01" "1984-01-01" "1984-04-01" "1984-07-01" "1984-10-01"
[17] "1985-01-01" "1985-04-01"
Hy guys,
I want to build in R a Date as:
02-06-year
for 15 years.
Here the code:
library(timeDate)
listHolidays
seq=0:5000
data.iniziale <- as.Date("2015-01-01")
calendario = data.iniziale + seq
l = length(calendario)
for (i in 1:l){
x[i]=as.Date(year(calendario[i]),06,02)
}
It does not work as is.
How can I do for that date
Similar to Albins solution, but I understood the question slightly different:
format(seq(as.Date("2015-01-01"), as.Date("2030-01-01"), "year"), "%Y-06-02")
Output:
[1] "2015-06-02" "2016-06-02" "2017-06-02" "2018-06-02" "2019-06-02" "2020-06-02" "2021-06-02"
[8] "2022-06-02" "2023-06-02" "2024-06-02" "2025-06-02" "2026-06-02" "2027-06-02" "2028-06-02"
[15] "2029-06-02" "2030-06-02"
I suggest to use some of existing functions of R to facilitate your task.
With the seq function, you can generate simply a sequence of dates. And format is as shown below:
format(seq(as.Date("2015-01-01"), as.Date("2030-01-01"), "days"), "%m-%d-%Y")
Output (partly):
[1] "01-01-2015" "01-02-2015" "01-03-2015" "01-04-2015" "01-05-2015" "01-06-2015" "01-07-2015" "01-08-2015"
[9] "01-09-2015" "01-10-2015" "01-11-2015" "01-12-2015" "01-13-2015" "01-14-2015" "01-15-2015" "01-16-2015"
Another possible solution, using lubridate:
library(tidyverse)
library(lubridate)
str_c("2-6-", 2000:2014) %>% dmy
#> [1] "2000-06-02" "2001-06-02" "2002-06-02" "2003-06-02" "2004-06-02"
#> [6] "2005-06-02" "2006-06-02" "2007-06-02" "2008-06-02" "2009-06-02"
#> [11] "2010-06-02" "2011-06-02" "2012-06-02" "2013-06-02" "2014-06-02"
I have a bunch of character vectors which I use to download some files (one for each month of the year), for which I have to change the date for every single link manually (at the end of the vector). It looks like this:
query_01_19 = "?format=Html&userId=1232&userHash=U127KfIHaiz3ks2gXEgNctA9n8P4c87o1SFcEu2weKpNdupQwmuRaMltEN7&query=ApplicationStatusByJob&from=01.01.2019&to=31.01.2019"
query_02_19 = "?format=Html&userId=1232&userHash=U127KfIHaiz3ks2gXEgNctA9n8P4c87o1SFcEu2weKpNdupQwmuRaMltEN7&query=ApplicationStatusByJob&from=01.02.2019&to=28.02.2019"
query_03_19 = "?format=Html&userId=1232&userHash=U127KfIHaiz3ks2gXEgNctA9n8P4c87o1SFcEu2weKpNdupQwmuRaMltEN7&query=ApplicationStatusByJob&from=01.03.2019&to=31.03.2019"
query_04_19 = "?format=Html&userId=1232&userHash=U127KfIHaiz3ks2gXEgNctA9n8P4c87o1SFcEu2weKpNdupQwmuRaMltEN7&query=ApplicationStatusByJob&from=01.04.2019&to=30.04.2019"
query_05_19 = "?format=Html&userId=1232&userHash=U127KfIHaiz3ks2gXEgNctA9n8P4c87o1SFcEu2weKpNdupQwmuRaMltEN7&query=ApplicationStatusByJob&from=01.05.2019&to=31.05.2019"
query_06_19 = "?format=Html&userId=1232&userHash=U127KfIHaiz3ks2gXEgNctA9n8P4c87o1SFcEu2weKpNdupQwmuRaMltEN7&query=ApplicationStatusByJob&from=01.06.2019&to=30.06.2019"
query_07_19 = "?format=Html&userId=1232&userHash=U127KfIHaiz3ks2gXEgNctA9n8P4c87o1SFcEu2weKpNdupQwmuRaMltEN7&query=ApplicationStatusByJob&from=01.07.2019&to=31.07.2019"
query_08_19 = "?format=Html&userId=1232&userHash=U127KfIHaiz3ks2gXEgNctA9n8P4c87o1SFcEu2weKpNdupQwmuRaMltEN7&query=ApplicationStatusByJob&from=01.08.2019&to=31.08.2019"
query_09_19 = "?format=Html&userId=1232&userHash=U127KfIHaiz3ks2gXEgNctA9n8P4c87o1SFcEu2weKpNdupQwmuRaMltEN7&query=ApplicationStatusByJob&from=01.09.2019&to=30.09.2019"
query_10_19 = "?format=Html&userId=1232&userHash=U127KfIHaiz3ks2gXEgNctA9n8P4c87o1SFcEu2weKpNdupQwmuRaMltEN7&query=ApplicationStatusByJob&from=01.10.2019&to=31.10.2019"
query_11_19 = "?format=Html&userId=1232&userHash=1277KfIHaiz3ks2gXEgNctA9n8P4c87o1SFcEu2weKpNdupQwmuRaMltEN7&query=ApplicationStatusByJob&from=01.11.2019&to=30.11.2019"
query_12_19 = "?format=Html&userId=1232&userHash=U127KfIHaiz3ks2gXEgNctA9n8P4c87o1SFcEu2weKpNdupQwmuRaMltEN7&query=ApplicationStatusByJob&from=01.12.2019&to=31.12.2019"
This is already rather tedious for one year, but it becomes a real pain if I want to this for all the following years (let's say until 2030).
Is there an easier way to do this?
Thanks in advance!
A few tricks to make this easy:
use of seq.Date to generate the first day of each month (it is shown here as seq due to the convenience R's S3 methods provide);
substract 1 from those to get the last day of the previous months; and
join those together with paste0 after formating them to the dot-separated date format.
## 1
dates <- seq(as.Date("2018-01-01"), as.Date("2019-01-01"), by = "month")
dates
# [1] "2018-01-01" "2018-02-01" "2018-03-01" "2018-04-01" "2018-05-01" "2018-06-01" "2018-07-01"
# [8] "2018-08-01" "2018-09-01" "2018-10-01" "2018-11-01" "2018-12-01" "2019-01-01"
dates_first <- format(dates[-length(dates)], format = "%d.%m.%Y")
## 2
dates_last <- format(dates[-1] - 1L, format = "%d.%m.%Y")
dates_last
# [1] "31.01.2018" "28.02.2018" "31.03.2018" "30.04.2018" "31.05.2018" "30.06.2018" "31.07.2018"
# [8] "31.08.2018" "30.09.2018" "31.10.2018" "30.11.2018" "31.12.2018"
## 3
paste0(
"?format=Html&userId=1232&userHash=U127KfIHaiz3ks2gXEgNctA9n8P4c87o1SFcEu2weKpNdupQwmuRaMltEN7&query=ApplicationStatusByJob&from=",
dates_first,
"&to=",
dates_last)
# [1] "?format=Html&userId=1232&userHash=U127KfIHaiz3ks2gXEgNctA9n8P4c87o1SFcEu2weKpNdupQwmuRaMltEN7&query=ApplicationStatusByJob&from=01.01.2018&to=31.01.2018"
# [2] "?format=Html&userId=1232&userHash=U127KfIHaiz3ks2gXEgNctA9n8P4c87o1SFcEu2weKpNdupQwmuRaMltEN7&query=ApplicationStatusByJob&from=01.02.2018&to=28.02.2018"
# [3] "?format=Html&userId=1232&userHash=U127KfIHaiz3ks2gXEgNctA9n8P4c87o1SFcEu2weKpNdupQwmuRaMltEN7&query=ApplicationStatusByJob&from=01.03.2018&to=31.03.2018"
# [4] "?format=Html&userId=1232&userHash=U127KfIHaiz3ks2gXEgNctA9n8P4c87o1SFcEu2weKpNdupQwmuRaMltEN7&query=ApplicationStatusByJob&from=01.04.2018&to=30.04.2018"
# [5] "?format=Html&userId=1232&userHash=U127KfIHaiz3ks2gXEgNctA9n8P4c87o1SFcEu2weKpNdupQwmuRaMltEN7&query=ApplicationStatusByJob&from=01.05.2018&to=31.05.2018"
# [6] "?format=Html&userId=1232&userHash=U127KfIHaiz3ks2gXEgNctA9n8P4c87o1SFcEu2weKpNdupQwmuRaMltEN7&query=ApplicationStatusByJob&from=01.06.2018&to=30.06.2018"
# [7] "?format=Html&userId=1232&userHash=U127KfIHaiz3ks2gXEgNctA9n8P4c87o1SFcEu2weKpNdupQwmuRaMltEN7&query=ApplicationStatusByJob&from=01.07.2018&to=31.07.2018"
# [8] "?format=Html&userId=1232&userHash=U127KfIHaiz3ks2gXEgNctA9n8P4c87o1SFcEu2weKpNdupQwmuRaMltEN7&query=ApplicationStatusByJob&from=01.08.2018&to=31.08.2018"
# [9] "?format=Html&userId=1232&userHash=U127KfIHaiz3ks2gXEgNctA9n8P4c87o1SFcEu2weKpNdupQwmuRaMltEN7&query=ApplicationStatusByJob&from=01.09.2018&to=30.09.2018"
# [10] "?format=Html&userId=1232&userHash=U127KfIHaiz3ks2gXEgNctA9n8P4c87o1SFcEu2weKpNdupQwmuRaMltEN7&query=ApplicationStatusByJob&from=01.10.2018&to=31.10.2018"
# [11] "?format=Html&userId=1232&userHash=U127KfIHaiz3ks2gXEgNctA9n8P4c87o1SFcEu2weKpNdupQwmuRaMltEN7&query=ApplicationStatusByJob&from=01.11.2018&to=30.11.2018"
# [12] "?format=Html&userId=1232&userHash=U127KfIHaiz3ks2gXEgNctA9n8P4c87o1SFcEu2weKpNdupQwmuRaMltEN7&query=ApplicationStatusByJob&from=01.12.2018&to=31.12.2018"
(Easily could have been done with sprintf or related functions.)
I have continuous daily data from 2015-12-01 to 2016-07-01.Since it's already been aggregated, these are unique dates with correspondent quantity.
However, whenever i convert the data into time series using ts() and plot.ts() it. The potting shows my tseries never reach 2016-07-01, but only stops around 2016-0. While ggplot of df data shows the ideal graph.
df
tseries = ts(df$F, start = c(2015,12,01), end = c(2016,07,01), frequency = 208)
plot.ts(tseries)
tseries
dfplotting
tldr; You misspecified the frequency (and start) arguments in ts; either provide correct frequency/start arguments, or use the zoo package instead.
There is a misunderstanding about the role of frequency in ts. The frequency argument depends on the format of your data. For example, if you have daily data for a year then frequency would be 365, since one year has 365 days (ignoring leap years). start then has to match the frequency of your data. For daily data, start needs to provide the day (out of 365) of your start date.
Let's generate a date sequence from 01/12/2015 until 01/07/2016 in 1 day steps.
dates <- seq(as.Date("2015/12/01"), as.Date("2016/07/01"), by = "1 day")
Next we generate some sample data for every day.
set.seed(2017);
x <- 100 - cumprod(1 + rnorm(length(dates), 0.01, 0.05))
We create a ts object and plot.
tseries <- ts(
x,
start = c(2015, as.numeric(format(dates[1], "%j"))),
frequency = 365)
plot.ts(tseries);
Note how as.numeric(format(dates[1], "%j")) is the day of the year that corresponds to the 01/12/2015; we can inspect tseries to confirm
tseries;
#Time Series:
#Start = c(2015, 335)
#End = c(2016, 183)
#Frequency = 365
# [1] 98.91829 98.91165 98.86055 98.94935 98.94251 98.90804 99.00404 98.99416
# [9] 98.99744 98.90906 98.87945 98.78015 98.81349 98.78344 98.85828 98.77868
# [17] 98.79591 98.70452 98.76475 98.80961 98.78934 98.82882 98.70564 98.79937
# [25] 98.74176 98.72526 98.73731 98.68448 98.72841 98.70941 98.74059 98.71282
# [33] 98.66201 98.71089 98.67883 98.45837 98.52216 98.36389 98.37027 98.37203
# [41] 98.44200 98.42452 98.46624 98.52957 98.53985 98.44419 98.42480 98.33157
# [49] 98.23825 98.11436 97.96746 98.10811 98.07050 98.05331 98.18894 98.26369
# [57] 98.10984 97.95076 97.95309 97.96750 97.86906 97.82784 97.95063 97.92361
# [65] 97.80495 97.75671 97.78848 97.70829 97.60183 97.67609 97.51230 97.57353
# [73] 97.62052 97.79321 97.57016 97.18006 97.25852 97.25187 97.13964 97.10216
# [81] 97.44889 97.22116 97.20823 97.16990 97.13855 97.10450 96.93565 96.79920
# [89] 96.82153 96.84342 96.86261 96.78734 97.08032 97.19380 97.30147 97.21610
# [97] 97.23463 97.39229 97.47829 97.56498 97.53851 97.34443 97.32700 97.18775
#[105] 97.29549 97.53480 97.44546 97.38659 97.23025 97.29599 97.36905 97.38267
#[113] 97.30787 97.36199 97.41452 97.31361 96.98821 96.92238 96.78418 96.87618
#[121] 96.78397 96.79244 96.80496 96.87619 97.00473 96.77162 96.61101 96.91671
#[129] 96.70001 96.75187 96.85348 96.77186 96.59937 96.72491 96.81188 96.80928
#[137] 96.71012 96.39952 96.49581 96.50415 96.56627 96.51843 96.72303 96.59825
#[145] 96.71873 96.65840 96.69296 96.86833 96.79887 96.71224 96.75685 96.76380
#[153] 96.63228 96.74893 96.51374 96.66589 96.61319 96.75718 96.62919 96.39169
#[161] 96.47066 96.59940 96.52173 96.50408 96.22667 96.03279 96.01529 95.80778
#[169] 95.77858 95.90350 96.15438 95.86239 95.99304 95.89340 95.70911 95.74620
#[177] 95.66125 95.81266 95.56044 95.36743 95.35368 95.54013 95.17587 95.07042
#[185] 94.67955 94.57888 94.57412 94.37998 94.36650 93.92591 93.82484 93.71398
#[193] 93.66040 93.60182 93.51263 93.54927 92.93701 92.72238 92.63081 92.85764
#[201] 92.61470 92.28663 92.28687 92.17086 92.31845 92.11827 91.72824 91.78567
#[209] 91.68942 91.34734 90.89702 91.32072 91.11538 90.83474
Note that the 335th day of 2015 corresponds to the 1st of December 2015, and the 183rd day of 2016 to the 1st of July 2016.
As an alternative we can also use the zoo package, which has the benefit that we don't have to work out the proper frequency (and start) arguments.
library(zoo);
tseries <- zoo(x, dates);
plot.zoo(tseries);
I have a time series in R that I would like to work with, spanning from 01-01-52 to 01-01-88. (1952 to 1988). 37 observations.
However, when I read it in in R, I encounter the problem that the observations from 01-01-52 to 01-01-68 are interpreted as being in 2052 etc., rather than 1952.
How do I force R to read in all the data as being from 1952 to 1988?
Link to my data: https://www.dropbox.com/s/93foyc238skt3xj/AgricIndus.csv?dl=0
This is the code I have used. Do you know what I need to do with my code to make it read properly?
agri <- read.table("AgricIndus.csv",
sep = ",", header = TRUE, skip = 0,
stringsAsFactors = FALSE)
agri$time <- as.Date(agri$time, "%m-%d-%y")
agri.xts <- xts(agri[, 2:3], order.by = agri$time)
One way (hack) can be the following:
agri$time <- as.Date(paste0(substring(agri$time,1,6), '19', substring(agri$time,7,8)),
"%m-%d-%Y")
agri$time
# [1] "01-01-52" "01-01-53" "01-01-54" "01-01-55" "01-01-56" "01-01-57" "01-01-58" "01-01-59" "01-01-60" "01-01-61" "01-01-62" "01-01-63" "01-01-64" "01-01-65"
# [15] "01-01-66" "01-01-67" "01-01-68" "01-01-69" "01-01-70" "01-01-71" "01-01-72" "01-01-73" "01-01-74" "01-01-75" "01-01-76" "01-01-77" "01-01-78" "01-01-79"
# [29] "01-01-80" "01-01-81" "01-01-82" "01-01-83" "01-01-84" "01-01-85" "01-01-86" "01-01-87" "01-01-88"
If you can be sure that your time series is regular then the it is probably the easiest to generate a regular date sequence like so:
agri$time <- seq.Date(as.Date("1952-01-01"),as.Date("1988-01-01"),by='years’)
Another easy solution that would work for irregular time series as well would be to read your data as years 52 to 88 with format = %m-%d-%Y (capitalized “Y” !) and add 1900 years:
df$time <- as.POSIXlt(as.Date(df$time,format = '%m-%d-%Y'))
df$time$year <-df$time$year + 1900
df$time <- as.Date(df$time)
df$time
[1] "1952-01-01" "1953-01-01" "1954-01-01" "1955-01-01"
[5] "1956-01-01" "1957-01-01" "1958-01-01" "1959-01-01"
[9] "1960-01-01" "1961-01-01" "1962-01-01" "1963-01-01"
[13] "1964-01-01" "1965-01-01" "1966-01-01" "1967-01-01"
[17] "1968-01-01" "1969-01-01" "1970-01-01" "1971-01-01"
[21] "1972-01-01" "1973-01-01" "1974-01-01" "1975-01-01"
[25] "1976-01-01" "1977-01-01" "1978-01-01" "1979-01-01"
[29] "1980-01-01" "1981-01-01" "1982-01-01" "1983-01-01"
[33] "1984-01-01" "1985-01-01" "1986-01-01" "1987-01-01"
[37] "1988-01-01"