Convert R character to date [duplicate] - r

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

Related

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 can I convert a "Month Year" string to a date object in R? [duplicate]

This question already has answers here:
Combine separate Year and Month columns into single Date Column
(5 answers)
Closed 1 year ago.
I am trying to combine full month name and year data to get Year-Month data, but I'm having some trouble getting R to read the Month as a date object. The example below returns all NAs in the 'Date' column after passing it through as.Date(). Is there a better way to go about this? Thanks!
Month <- c('January','July','January')
Year <- c('2020','2020','2021')
df <- data.frame(Month, Year)
df$Date <- paste(df$Month, df$Year)
df$Date <- as.Date(df$Year, format = "%B %Y")
df$Date
We need a day as well to create the Date
df$Date <- as.Date(paste(df$Date, 1), format = "%B %Y %d")

Need help converting date format [duplicate]

This question already has an answer here:
R formatting a date from a character mmm dd, yyyy to class date [duplicate]
(1 answer)
Closed 5 years ago.
I have a csv file where the dates look like this: Jan 31, 2017
I would like it be 2017-01-31
I've been searching the site and I found a lot of similar questions but none with my strange date format.
EDIT: Was unclear. I need to change a lot of dates so doing it manually won't work. Thanks for suggestions, reading the help function of lubridate and strptime now.
Use the lubridate library:
library(lubridate)
date <- "Jan 31, 2017"
date2 <- mdy(date)
date2
[1] "2017-01-31"

Factor to date format [duplicate]

This question already has answers here:
Converting year and month ("yyyy-mm" format) to a date?
(9 answers)
Closed 7 years ago.
I have date in jan-16 format. They are in factor data type. R is recognizing them as character. I want these dates to be arranged in calendar sequence. When I try to plot a graph it gives me in alphabetical order which I don't want. I am getting an error saying
Error in CharToDate(x) : character string is not in a standard unambiguous format.
You can convert your factor date vector to Date type, and then plot it. Most R plotting packages should be able to order numerically on the date.
dates.raw <- c("Jan 16 2015", "Jan 16 2016")
dates.formatted <- as.Date(dates.raw, format = "%B %d %Y")
> dates.formatted
[1] "2015-01-16" "2016-01-16"

Date format years-months in R [duplicate]

This question already has answers here:
Converting year and month ("yyyy-mm" format) to a date?
(9 answers)
Closed 9 years ago.
I have a data in the following format 200101 and I want it to be in the following format 2001-01 or 2001/01
Thanks
I don't deal with dates so there may well be better approaches. Your problem is you have no day. I know the zoo package can handle this but not in the format you want. I also give a regex approach but this is not a date class, just character.
As date:
library(zoo)
as.yearmon("200101", "%Y%m")
## > as.yearmon("200101", "%Y%m")
## [1] "Jan 2001"
As character:
gsub("([0-9]{4})","\\1-", "200101")
## > gsub("([0-9]{4})","\\1-", "200101")
## [1] "2001-01"
## gsub("([0-9]{4})","\\1/", "200101")

Resources