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"
Related
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")
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)
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"
How do I convert a string "Apr-16" to Month April of 2016 ?
I tired as.Date(x, format ..) , that isn't helping .
What options do I have here ?
x <- "Apr-16"
x <- as.Date(paste0("1-",x),format="%d-%b-%y")
format(x,"%B of %Y")
[1] "April of 2016"
Another option is regex
sub("-", "il of 20", x)
#[1] "April of 2016"
Or using yearmon from zoo
library(zoo)
format(as.Date(as.yearmon(x, "%b-%y")), "%B of %Y")
#[1] "April of 2016"
data
x <- "Apr-16"
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"