Convert month year to a date in r [duplicate] - r

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")

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 i format date column with format "Sat Aug 16 2001" using R? [duplicate]

This question already has answers here:
Convert date-time string to class Date
(4 answers)
Closed 1 year ago.
I have a dataframe with a date column using a specific date pattern, where the format is "Sat Aug 15 1992", for example.
I tried to use as.Date("Sat Aug 15 1992","%Y-%m-%d") but it returns only NA. How can I make R understand my column as a date?
Check ?strptime, you can use :
as.Date("Sat Aug 15 1992","%a %b %d %Y")
#[1] "1992-08-15"
Try
as.Date("Sat Aug 15 1992",format="%Y-%m-%d")

Dealing with month abbreviations when converting to a date

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")

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"

Convert R character to date [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Converting year and month to a date in R?
I have the following string:
date <- "Feb 1964"
I am trying to convert this to a date, using the following code:
new.date <- as.Date(date, format="%b %Y")
NA is returned here. Can someone kindly explain how I can achieve this, and explain what I am doing wrong ..
Many thanks in advance
I think this might do what you want.
date <- "Feb 1964"
date <- "1 Feb 1964"
new.date <- as.Date(date, format="%d %b %Y")
new.date

Resources