Converting written to numeric dates in R - r

What's the most elegant way to convert these example dates to numeric dates:
dates <- c("April 1, 2017", "June 27, 2017", "September 24, 2017")
I would like this as a result:
"01-04-2017", "27-06-2017", "24-09-2017"

Using base
as.Date(dates, format = "%B %d, %Y")
[1] "2017-04-01" "2017-06-27" "2017-09-24"
and then formatted
format(as.Date(dates, format = "%B %d, %Y"), "%d-%m-%Y")
[1] "01-04-2017" "27-06-2017" "24-09-2017"

You could use mdy function of lubridate package to parse dates. Then use format to convert it to desired form.
library(lubridate)
format(mdy(dates), "%d-%m-%Y")
#[1] "01-04-2017" "27-06-2017" "24-09-2017"

Related

how to change a date format in R

I need help in R program to change date format from "May 29, 2015" to "05,29,2015".I tried this "housing$SaleDate<- as.Date(housing$SaleDate,"%Y%M%D")" but did not work. Thanks
After converting to Date class, we may need format i.e.
library(lubridate)
str1 <- "May 29, 2015"
format(mdy(str1), '%m,%d,%Y')
[1] "05,29,2015"
For the OP's as.Date, it would be
housing$SaleDate <- format(as.Date(housing$SaleDate, "%b %d, %Y"), "%m,%d,%Y")

Convert date to specific format as Month Date, year

Is there way to convert the below code into Mar 10, 2020?
weekdays.Date(as.Date("2020-03-10", format="%Y-%m-%d"))
I think this should work:
format(as.Date("2020-03-10"), "%b %d, %Y")
# "Mar 10, 2020"

convert "2014-05" into date format as "May 2015" for display in ggplot in R

I have date in this character format "2017-03" and I want to convert it in "March 2017" for display in ggplot in R. But when I try to convert it using as.Date("2017-03","%Y-%m") it gives NA
You can consider using zoo::as.yearmon function as:
library(zoo)
#Sample data
v <- c("2014-05", "2017-03")
as.yearmon(v, "%Y-%m")
#[1] "May 2014" "Mar 2017"
#if you want the month name to be in full. Then you can format yearmon type as
format(as.yearmon(v, "%Y-%m"), "%B %Y")
#[1] "May 2014" "March 2017"
Parse dates back and forth can be done like this:
The one you mentioned is done by quoting MKR:
Use zoo package
library(zoo)
date <- "2017-03"
as.yearmon(date, "%Y-%m")
#[1] "Mar 2017"
format(as.yearmon(date, "%Y-%m"), "%B %Y")
#[1] "March 2017"
If you want to parse March 2017 or other similar formats back to 2017-03:
Use hms package because base R doesn't provide a nice built-in class for date
library(hms)
DATE <- "March 1 2017"
parse_date(DATE, "%B %d %Y")
#[1] "2017-03-01"
Or if you are parsing dates with foreign language:
foreign_date <- "1 janvier 2018"
parse_date(foreign_date, "%d %B %Y", locale = locale("fr"))
#[1] "2018-01-01"
By using the locale = locale("language") you can parse dates with foreign months names to standard dates. Use this to check the language:
date_names_langs()
-Format:
-Year: %Y(4 digits) %y(2 digits; 00-69->2000-2069, 70-99 -> 1970-1999)
-Month: %m (2 digits), %b (abbreviation: Jan), %B full name January
-Day: %d (2 digits)

Conversion of date format %B %Y

Can we somehow convert dates such as "November 2017", "December 2017" to date? I tried to import csv data, but received factor columns.
I tried the following code, but was not successful.
as.POSIXct(as.character(dat$Date), format = "%B %Y")
A POSIXct date needs the day of the month to be complete and valid.
You can add it to the date strings, then use the format "%B %Y %d" e.g. :
as.POSIXct(paste(as.character(dat$Date),"01"), format = "%B %Y %d")
BTW, when you import a csv you can set stringsAsFactors=FALSE (as argument of read.csv/read.table functions) to obtain characters instead of factors.
The argument truncated does the job:
library(lubridate)
myd("November 2017", truncated = 1)
# "2017-11-01"
A quick solution from lubridate package
dmy(paste("01", dat$Date))
A proposition with the zoo package:
Sys.setlocale("LC_TIME","English")
#> [1] "English_United States.1252"
zoo::as.yearmon("November 2017", '%B %Y')
#> [1] "Nov 2017"
zoo::as.Date(zoo::as.yearmon("November 2017", '%B %Y'))
#> [1] "2017-11-01"
Sys.setlocale("LC_TIME","French")
#> [1] "French_France.1252"
zoo::as.yearmon("Novembre 2017", '%B %Y')
#> [1] "nov. 2017"
# Created on 2021-02-03 by the reprex package (v0.3.0.9001)
Regards,

R formatting a date from a character mmm dd, yyyy to class date [duplicate]

This question already has answers here:
Convert date-time string to class Date
(4 answers)
Closed 6 years ago.
I am trying to format a date from a character of form mmm dd, yyyy
I tried :
date1 <- "Dec 05, 2016"
date2 <- format(date1, format="%d %B %Y")
class(date2)
date2
but this does not work.
Thank you for any help.
I then tried as suggested:
date1 <- "Dec 05, 2016"
date2 <- format(as.Date(date1, "%b %d, %Y"), "%d %B %Y")
date2 <- as.Date(date2)
class(date2)
date2
but this still did not covert to the class date but was class: character
This seems to work
ref:Convert character to Date in R
library(lubridate)
date1 <- "Dec 05, 2016"
date2 <- mdy(date1)
class(date2)
date2
We can convert to 'Date' class and then convert to the format of interest
format(as.Date(date1, "%b %d, %Y"), "%d %B %Y")
#[1] "05 December 2016"

Resources