Dealing with month abbreviations when converting to a date - r

I'm trying to convert a column to a date and am having some trouble. The dates are a in a column named month and each value is an abbreviated month followed by a year, like "Nov 2016" and "Mar 2017". What's the best way to convert this column to a date so I can use it as the x-axis on a graph.
Thanks!

This issue comes around a lot on Stack Overflow. Basically: month + year is not a date - you need a day too.
The usual solutions are (1) use zoo::as.yearmon to make a "year month" object:
library(zoo)
as.yearmon("Nov 2016", "%b %Y")
or (2) use the first of the month as an arbitrary day to create a date:
as.Date(paste("01", "Nov 2016"), "%d %b %Y")

Related

Can't use zoo's as.yearmon properly, the transformed column has years showing up like 0086 instead of 1986

Hello and thanks for being here.
I'm trying to convert dates with as.yearmon but the results I'm getting are odd and I do not know how to fix this; I tried searching on here and on the offical package guide without finding someone with the same problem.
The problem is that I transformed a column of a dataset which was formatted as "month/year" with as.yearmon but the results were not correct.
For example, the first 3 values of the column of the original DF are: "1/86", "2/86", "3/86".
After using this function to convert them:
library(zoo)
Dates <- Returns
Dates$Month<- zoo::as.yearmon(Dates$Month, "%m / %Y")
[Where "Returns" is the original dataframe and Dates the new one with the modified dates.]
The result I got, instead of being: "gen 1986", "feb 1986", "mar 1986" was "gen 0086", "feb 0086", "mar 0086" and I don know why.
[I should not that "gen", "feb", "mar" are in Italian; I do not know if that matters and I do not know how to change that to "Jan", "Feb", "Mar" which I think I'll have to do as well]
Thanks in advance for your help, if something is not clear just let me know; I'm still a rookie.
You need to use "%m/%y" instead of "%m/%Y". Your dates don't have a the full year notation.
x <- c("1/86", "2/86", "3/86")
zoo::as.yearmon(x, "%m/%y")
[1] "jan 1986" "feb 1986" "mrt 1986"
Date functions return the names in the local locale. If you want them in English:
Sys.setlocale("LC_TIME", "English")
zoo::as.yearmon(x, "%m/%y")
[1] "Jan 1986" "Feb 1986" "Mar 1986"
Everytime you restart R, this will be set back to your locale.
More info here on SO

How can as.Date() convert fully written dates into ISO 8601? [duplicate]

This question already has an answer here:
Format for ordinal dates (day of month with suffixes -st, -nd, -rd, -th)
(1 answer)
Closed 1 year ago.
I currently have a vector of dates that are in the following format:
a <- c("Wednesday 26th May 2021","Thursday 27th May 2021")
I've tried to get it into ISO 8601 using the following:
as.Date(a, "%I %d%S %F %Y")
But I'm not 100% certain about the syntax of writing dates.
Any thoughts are appreciated!
You can remove the date suffixes and use as.Date -
#Added an extra date that does not have th as prefix.
a <- c("Wednesday 26th May 2021","Thursday 27th May 2021",
'Tuesday 1st June 2021', 'Monday 31st May 2021')
as.Date(sub('(?<=\\d)(th|rd|st|nd)', '', a, perl = TRUE), '%A %d %b %Y')
#[1] "2021-05-26" "2021-05-27" "2021-06-01" "2021-05-31"
Read ?strptime for different format specification.
If you are open to packages lubridate::dmy works directly.
lubridate::dmy(a)
#[1] "2021-05-26" "2021-05-27" "2021-06-01" "2021-05-31"

How to convert full date character into mm/dd/yyyy in R?

I have a dataset in R with a date column that is currently being treated as a character column. The dates in this column are listed as "August 24 2012" and I am trying to convert it into a date format such as 08/24/2012.
I am a novice with R and have tried to use format() and lubridate with no success. How do I convert these dates from a character to date?
We need to convert it to Date class and then use format
format(as.Date(dates, "%B %d %Y"), "%m/%d/%Y")
#[1] "08/24/2012"
As the order is month, day, year use mdy from lubridate
library(lubridate)
format(mdy(dates), "%m/%d/%Y")
#[1] "08/24/2012"
It is based on the order, e.g.
ydm("2012 24 August")
#[1] "2012-08-24"
data
dates <- "August 24 2012"

R lubridate. What goes wrong?

Tried to make a factor to date.
X:
Jul-15
Jul-15
Jul-15
Jul-15
Aug-16
I want to convert them to a date.
X <- as.Date(Xx, format="%b-%y")
Results in NA's.
This was my source for the code: https://www.r-bloggers.com/date-formats-in-r/
as.Date is the base R function, as_date is the lubridate function, but this is not your problem
Without a day, you're not completely specifying the date completely. Unlike year, where R will fill in with the current year, this does not appear to happen for a missing day
As a workaround, run:
X <- as.Date(paste0(Xx,'-01'), format="%b-%y-%d")
Which will set the dates to be the first of the month
You need a complete date, with day :
library(lubridate)
X <- "Jul-15"
X <- paste("01", X, sep="-")
dmy(X)
The yearmon function from the zoo package will help you here. Since you have lubridate as a tag on the question, we can then use as_date from the lubridate package to turn it into a date object. The base function as.Date will work as well.
library(zoo)
library(lubridate)
as_date(as.yearmon(Xx, "%b-%y"))
[1] "2015-07-01" "2015-07-01" "2015-07-01" "2015-07-01" "2016-08-01"
If you want to leave them as yearmon class, you can just run:
as.yearmon(Xx, "%b-%y")
[1] "Jul 2015" "Jul 2015" "Jul 2015" "Jul 2015" "Aug 2016"

Convert month year to a date in r [duplicate]

This question already has answers here:
Converting year and month ("yyyy-mm" format) to a date?
(9 answers)
Closed 5 years ago.
I need to convert following types of strings to a date format.
Convert "Feb 2009" to 2009-02-01
Convert "Jan 2010" to 2010-01-01
Convert "Mar 2011" to 2011-03-01
I can achieve this from the following code using zoo package.
as.Date(as.yearmon("Feb 2009"))
But due to some constraints I do not want to use this way of converting. So I want to know if there is any other way in R of achieving this task?
You can paste 01 to the vector using paste and then convert to date by specifying the appropriate format
as.Date(paste('01', v1), format='%d %b %Y')
#[1] "2009-02-01" "2010-01-01", "2011-03-01"
data
v1 <- c("Feb 2009", "Jan 2010", "Mar 2011")

Resources