as.Date function giving incorrect year [duplicate] - r

This question already has answers here:
Convert string to date, format: "dd.mm.yyyy"
(4 answers)
Closed last year.
as.Date('28/3/2021', '%d/%m/%y')
gives output:
[1] "2020-03-28"
How do I write the code to ensure the year is correct i.e. 2021, not 2020?

Here is the solution + explanation:
> # %Y is used for 4 digit numbers
>
> as.Date('28/3/2021', '%d/%m/%Y')
[1] "2021-03-28"
>
> # while %y for 2 digits
>
> as.Date('28/3/21', '%d/%m/%y')
[1] "2021-03-28"
>

Related

Get Date objects back from min(dates):max(dates) in R? [duplicate]

This question already has answers here:
Creating a unique sequence of dates
(6 answers)
Create a Vector of All Days Between Two Dates
(3 answers)
Closed 7 months ago.
Some R code:
> dates <- as.Date(c('2020-01-01', '2020-01-02'))
> min(dates)
[1] "2020-01-01"
> max(dates)
[1] "2020-01-02"
> min(dates):max(dates)
[1] 18262 18263
> as.Date(min(dates):max(dates))
Error in as.Date.numeric(min(dates):max(dates)) :
'origin' must be supplied
> as.Date(min(dates):max(dates), origin="1970-01-01")
[1] "2020-01-01" "2020-01-02"
This shows that min and max are working as expected, but when I put them in a range, the dates turn into integers. How do I prevent that?
I can just use the "origin", but it seems like a hack.
Instead of using : and then reconverting the coerced numeric storage to Date class, use the seq which already have a method for Date class
seq(min(some_dates), max(some_dates), by = "1 day")
[1] "2020-01-01" "2020-01-02"

Convert a date time character string to date - YYYYMM format [duplicate]

This question already has answers here:
converting datetime string to POSIXct date/time format in R
(1 answer)
How to convert string "MMM DD YYYY" to date YYYY-MM-DD
(1 answer)
Closed 7 months ago.
I have a character string date time string but need to convert the same into YYYYMM date format. Cannot seem to find a solution as all functions are converting into NA or weird date format.
Date_format_current <- '02/09/2020 23:35'
You can use the following code
library(lubridate)
library(zoo)
current <- '02/09/2020 23:35'
as.yearmon(dmy_hm(current))
#> [1] "Sep 2020"
#Or
format(dmy_hm(current), "%Y-%m")
#> [1] "2020-09"
#Or
format_ISO8601(dmy_hm(current), precision = "ym")
#> [1] "2020-09"

String format parsing in R [duplicate]

This question already has answers here:
How to convert dd/mm/yy to yyyy-mm-dd in R
(6 answers)
Closed 3 years ago.
When I parse a character string into a date, Why does this not throw an error or an NA? I have tried the following
t <- "31-Oct-2012"
as.Date(t, format = "%d-%B-%Y") # this produces the expected result
as.Date(t, format = "%d-%B-%y") # I was expecting an NA
Instead I get
[1] "2020-10-31"
Because %y is for two digit year, so it takes only first two digits and ignores the rest. It treats t as
as.Date("31-Oct-20", format = "%d-%B-%y")
#[1] "2020-10-31"
This also works when you have anything after 2-digit year. See
as.Date("31-Oct-20ABC", format = "%d-%B-%y")
#[1] "2020-10-31"
R tries to "auto-complete" when there is less information, it returns some (incorrect) date for
as.Date("31-Oct-20", format = "%d-%B-%Y")
#[1] "0020-10-31"
but returns NA for
as.Date("31-Oct-ABC20", format = "%d-%B-%y")
#[1] NA

R - how to get the end date of the week and month from a given date? [duplicate]

This question already has answers here:
Create end of the month date from a date variable
(9 answers)
Closed 3 years ago.
How, in R, to get end dates of week, fortnight(beginning date too), month, quarter and year.
Let's say, we have date is "July 15th, 2017" and below code gets beginning dates.
> (a_date <- as.Date("15-07-17", "%d-%m-%y"))
[1] "2017-07-15"
> (beginning_date_week <- as.Date(cut(a_date, "week")))
[1] "2017-07-10"
> (beginning_date_month <- as.Date(cut(a_date, "month")))
[1] "2017-07-01"
> (beginning_date_quarter <- as.Date(cut(a_date, "quarter")))
[1] "2017-07-01"
> (beginning_date_year <- as.Date(cut(a_date, "year")))
[1] "2017-01-01"
adding 7 days to beginning date of week may get me the end date of week.
But, for month addition is simply not elegant(since some months have 30 days, some 31, some 28(29 some times)) and it only gets worse for quarters and years.
ceiling_date in the lubridate package returns the first date of the following period. Subtract 1 to get the last date of the current period.
library(lubridate)
a_date <- as.Date("15-07-17", "%d-%m-%y")
ceiling_date(a_date, "week", week_start = getOption("lubridate.week.start", 1))-1
[1] "2017-07-16"
ceiling_date(a_date, "month")-1
[1] "2017-07-31"
ceiling_date(a_date, "quarter")-1
[1] "2017-09-30"
ceiling_date(a_date, "year")-1
[1] "2017-12-31"

How to convert to a character date into standard date format in R [duplicate]

This question already has an answer here:
Convert R character to date [duplicate]
(1 answer)
Closed 4 years ago.
I have a csv file (df) with dates as " Mar-97, Apr-97..." . After importing to r with read.csv and stringAsFactors = F, the class(dates) is character.
I have tried : df$dates <- as.Date(df$Dates , format = "%d-%b-%y") & as.Date(df$Dates , format = "%b-%y"). class is converted to Date but it shows NA values?
you can try lubridate library:
library(lubridate)
> parse_date_time("Mar-97", "m y")
[1] "1997-03-01 UTC"
and you can vectorize
df=c("Mar 17","Apr 17")
> parse_date_time(df, "m y")
[1] "2017-03-01 UTC" "2017-04-01 UTC"

Resources