Conversion of date format %B %Y - r

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,

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

R returns NA when I try to coerce a string to date using as.Date [duplicate]

This question already has answers here:
strptime, as.POSIXct and as.Date return unexpected NA
(2 answers)
Closed 5 years ago.
I am trying to convert "March 15, 2017" to date.
as.Date("March 15, 2017", "%B %d, %Y") and it returned NA
I feel that the syntax fits well, what is the problem?
You are close, but have been bitten by your locale. If you look at the documentation for strptime, you will notice that
%B Full month name in the current locale. (Also matches abbreviated name on input.)
This is also the case for my system since Slovenian doesn't have English month names:
> as.Date("March 15, 2017", "%B %d, %Y")
[1] NA
> Sys.getlocale()
[1] "LC_COLLATE=Slovenian_Slovenia.1250;LC_CTYPE=Slovenian_Slovenia.1250;LC_MONETARY=Slovenian_Slovenia.1250;LC_NUMERIC=C;LC_TIME=Slovenian_Slovenia.1250"
What you can do is change locale, perhaps only for the duration of the conversion.
> Sys.setlocale(locale = "English")
[1] "LC_COLLATE=English_United States.1252;LC_CTYPE=English_United States.1252;LC_MONETARY=English_United States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252"
> as.Date("March 15, 2017", "%B %d, %Y")
[1] "2017-03-15"
And then back to normal
> Sys.setlocale(locale = "Slovenian")
[1] "LC_COLLATE=Slovenian_Slovenia.1250;LC_CTYPE=Slovenian_Slovenia.1250;LC_MONETARY=Slovenian_Slovenia.1250;LC_NUMERIC=C;LC_TIME=Slovenian_Slovenia.1250"
> as.Date("March 15, 2017", "%B %d, %Y")
[1] NA
But if I use a Slovenian name for March:
> as.Date("Marec 15, 2017", "%B %d, %Y")
[1] "2017-03-15"
Locale name will depend on your operating system, see ?Sys.setlocale for more info.

Converting written to numeric dates in 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"

Convert numeric data such as "715" into Date "July-2015" in R

I would like to friendly ask a question about converting numeric data into Date format.
I would like to convert the numeric data like:
time1<-c(715, 1212, 0416)
to
July-2015, Dec-2012, Apr-2016
I have tried these code but it is not working.
time2<-as.Date(as.character(time1), format="%m%y")
Does anyone have some ideas to solve this issue?
Part of the issue is that "July 2015", "December 2012", and "April 2016" are not dates since the specific day is missing. Another approach is to convert to zoo::yearmon. Here, the numeric input needs to be converted to a string with leading zero so that the month is from 01 to 12:
library(zoo)
ym <- as.yearmon(sprintf("%04d",time1),format="%m%y")
ym
##[1] "Jul 2015" "Dec 2012" "Apr 2016"
The result is of class yearmon, which can then be coerced to Date:
class(ym)
##[1] "yearmon"
d <- as.Date(ym)
d
##[1] "2015-07-01" "2012-12-01" "2016-04-01"
class(d)
##[1] "Date"
Try lubridate::parse_date_time():
library(lubridate)
time2 <- parse_date_time(time1, orders = "my")
format.Date(time2, "%b-%Y")
[1] "juil.-2015" "déc.-2012" "avril-2016" # my locale lang is French

Resources