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"
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"
I am trying to find out closest next quarter end date for a given date in R.
For example, if the input is "2022-02-23", the output should be "2022-03-31"
and if the input is "2022-03-07", the output should be "2022-06-30".
If the input is "2021-12-15", the output should be "2022-03-31".
Is there any function in R for this?
lubridate::quarter with argument type = "date_last" will get you most of the way there. From the comments, it looks like you want to jump to the following quarter if the date is in the last month of a quarter; we can achieve this by adding a month to each date before passing to quarter. We can add months safely using the %m+% operator.
library(lubridate)
dates_in <- ymd(c("2022-02-23", "2022-03-07", "2021-12-15"))
dates_out <- quarter(dates_in %m+% months(1), type = "date_last")
dates_out
# "2022-03-31" "2022-06-30" "2022-03-31"
Please see this kind of function using lubridate's quarter function
last_day_in_quarter <- function(d){
require(lubridate)
last_month_in_quarter <- ymd(paste(year(d),quarter(d)*3,1))
return(last_month_in_quarter %m+% months(1) - 1)
}
last_day_in_quarter(ymd("2021-12-15")) #"2021-12-31"
last_day_in_quarter(ymd("2022-02-15")) #"2022-03-31"
last_day_in_quarter(ymd("2021-05-15")) #"2021-06-30"
last_day_in_quarter(ymd("2021-07-15")) #"2021-09-30"
I think these kinds of problems become immensely easier to understand if you work with a true year-quarter-day type. There is one of these in the clock package (I am the author).
library(clock)
x <- date_parse(c("2022-02-23", "2022-03-07", "2021-12-15"))
x
#> [1] "2022-02-23" "2022-03-07" "2021-12-15"
# What quarter are we in now?
yqd <- as_year_quarter_day(x)
yqd
#> <year_quarter_day<January><day>[3]>
#> [1] "2022-Q1-54" "2022-Q1-66" "2021-Q4-76"
# Is the current month the same as the end-of-quarter month?
# (if so, we are going to shift forward by 1 quarter).
shift <- get_month(x) == get_month(as.Date(set_day(yqd, "last")))
shift
#> [1] FALSE TRUE TRUE
# Shift by 1 quarter where applicable
yqd[shift] <- yqd[shift] + duration_quarters(1)
yqd
#> <year_quarter_day<January><day>[3]>
#> [1] "2022-Q1-54" "2022-Q2-66" "2022-Q1-76"
# Set day to end of quarter
yqd <- set_day(yqd, "last")
yqd
#> <year_quarter_day<January><day>[3]>
#> [1] "2022-Q1-90" "2022-Q2-91" "2022-Q1-90"
# Now convert back to Date
as.Date(yqd)
#> [1] "2022-03-31" "2022-06-30" "2022-03-31"
I'm trying to insert the previous date for every date in a vector in R.
This is my current vector:
[1] "1990-02-08" "1990-03-28" "1990-05-16" "1990-07-05" "1990-07-13" "1990-08-22" "1990-10-03"
[8] "1990-10-29" "1990-11-14" "1990-12-07" "1990-12-18" "1991-01-08" "1991-02-01" "1991-02-07"
I'm trying to get the following:
[1] "1990-02-07" "1990-02-08" "1990-03-27" "1990-03-28" "1990-05-15" "1990-05-16" "1990-07-05"
ect.
I tried the following:
dates_lagged = as.Date(dates)-1
dates_combined = c(date, dates_lagged)
However, with this method, some dates are not getting lagged.
Is there a better way to do this?
Edit: to answer the comment, this is my code (replaced CSV with its starting values):
FOMC <- read_csv(file = c("x", "1990-02-08", "1990-03-28", "1990-05-16", "1990-07-05", "1990-07-13", "1990-08-22", "1990-10-03",
"1990-10-29", "1990-11-14", "1990-12-07"))
FOMC$x <- as.Date(FOMC$x, format = "%Y-%m-%d")
colnames(FOMC) <- "Date"
dates_vector <- FOMC[["Date"]]
FOMC = as.vector(as.Date(dates_vector))
dates_lagged = as.Date(FOMC)-1
dates_combined = c(FOMC, dates_lagged)
as.Date(dates_combined)
For some reason, there is no "1990-10-28" before "1990-10-29" for example, and I can't figure out why.
You could try:
as.Date(c(rbind(dates - 1, dates)), origin = "1970-01-01")
#> [1] "1990-02-07" "1990-02-08" "1990-03-27" "1990-03-28" "1990-05-15"
#> [6] "1990-05-16" "1990-07-04" "1990-07-05" "1990-07-12" "1990-07-13"
#> [11] "1990-08-21" "1990-08-22" "1990-10-02" "1990-10-03" "1990-10-28"
#> [16] "1990-10-29" "1990-11-13" "1990-11-14" "1990-12-06" "1990-12-07"
#> [21] "1990-12-17" "1990-12-18" "1991-01-07" "1991-01-08" "1991-01-31"
#> [26] "1991-02-01" "1991-02-06" "1991-02-07"
Data
dates <- c("1990-02-08", "1990-03-28", "1990-05-16", "1990-07-05", "1990-07-13",
"1990-08-22", "1990-10-03", "1990-10-29", "1990-11-14", "1990-12-07",
"1990-12-18", "1991-01-08", "1991-02-01", "1991-02-07")
dates <- as.Date(dates)
Created on 2021-11-04 by the reprex package (v2.0.0)
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"
I need to write some code in R that builds a string by looping over dates and I cant' seem to find an example of this in my books or by Googling. Basically:
for theDate = 1Jan14 to 31Dec14{
"http://website.com/api/" + theDate
}
I thought about creating an input file that held the dates, but that seems inelegant.Does anybody know of a better solution?
This doesn't consume that much memory and doesn't need the julian function:
start <- as.Date("01-08-14",format="%d-%m-%y")
end <- as.Date("08-09-14",format="%d-%m-%y")
theDate <- start
while (theDate <= end)
{
print(paste0("http://website.com/api/",format(theDate,"%d%b%y")))
theDate <- theDate + 1
}
.
[1] "http://website.com/api/01Aug14"
[1] "http://website.com/api/02Aug14"
[1] "http://website.com/api/03Aug14"
[1] "http://website.com/api/04Aug14"
[1] "http://website.com/api/05Aug14"
[1] "http://website.com/api/06Aug14"
[1] "http://website.com/api/07Aug14"
[1] "http://website.com/api/08Aug14"
[1] "http://website.com/api/09Aug14"
[1] "http://website.com/api/10Aug14"
[1] "http://website.com/api/11Aug14"
[1] "http://website.com/api/12Aug14"
[1] "http://website.com/api/13Aug14"
[1] "http://website.com/api/14Aug14"
[1] "http://website.com/api/15Aug14"
[1] "http://website.com/api/16Aug14"
[1] "http://website.com/api/17Aug14"
[1] "http://website.com/api/18Aug14"
[1] "http://website.com/api/19Aug14"
[1] "http://website.com/api/20Aug14"
[1] "http://website.com/api/21Aug14"
[1] "http://website.com/api/22Aug14"
[1] "http://website.com/api/23Aug14"
[1] "http://website.com/api/24Aug14"
[1] "http://website.com/api/25Aug14"
[1] "http://website.com/api/26Aug14"
[1] "http://website.com/api/27Aug14"
[1] "http://website.com/api/28Aug14"
[1] "http://website.com/api/29Aug14"
[1] "http://website.com/api/30Aug14"
[1] "http://website.com/api/31Aug14"
[1] "http://website.com/api/01Sep14"
[1] "http://website.com/api/02Sep14"
[1] "http://website.com/api/03Sep14"
[1] "http://website.com/api/04Sep14"
[1] "http://website.com/api/05Sep14"
[1] "http://website.com/api/06Sep14"
[1] "http://website.com/api/07Sep14"
[1] "http://website.com/api/08Sep14"
>
You can use
> dates <- seq(as.Date("2014-01-01"), as.Date("2014-12-31"), by=1)
to generate a vector of consecutive days. What you want to do with this is not entirely clear from your pseudo-code, but you can iterate directly over the vector (which is generally not what you want in R)
> for (d in dates) {
# Code goes here.
}
The comment-solution by #Roland will give you a vector of the form:
> paste0("http://website.com/api/", dates)
[1] "http://website.com/api/2014-01-01" "http://website.com/api/2014-01-02"
[3] "http://website.com/api/2014-01-03" "http://website.com/api/2014-01-04"
[5] "http://website.com/api/2014-01-05" "http://website.com/api/2014-01-06"
...
Of course after I ask the question I happen to find this.
days <- seq(from=as.Date('2011-02-01'), to=as.Date("2011-03-02"),by='days' )
for ( i in seq_along(days) )
{
print(paste(days[i],"T12:00:00", sep=""))
}
You could translate your date into julian days and then write a loop based on the julian days.
To convert to julian days you can use the code described here
And then you could write code using the the julian days like:
tmp <- as.POSIXlt("1Jan14", format = "%d%b%y")
strdate <- julian(tmp)
tmp <- as.POSIXlt("31Dec14", format = "%d%b%y")
enddate <- julian(tmp)
for (theDate in strdate:enddate){
paste ("http://website.com/api/", toString(theDate), sep = "")
}
you have to figure out how to convert back. I am not to sure about the julian function. maybe you should also have a look into "yday" of lubridate package.