Sorry about another date-question, but I couldn't find an answer.
I have a date-type "September 2017" and need to convert it to "30.09.2017" (last day of month — for all months of all years).
I've tried:
date <- as.Date(date), "%B %d %Y")
+ variations of %d%m%y
And that:
library(zoo)
date <- as.Date(as.yearmon(date))
But every time I have NA.
This works for me:
> library(zoo)
> s = "September 2017"
> as.yearmon(s)
[1] "Sep 2017"
If I then convert to date, it gets the first of the month:
> as.Date(as.yearmon(s))
[1] "2017-09-01"
This seems to be exactly what you are doing but you don't explicitly show us where your "September 2017" string is so I suspect a problem there...
As for the "30th", what are you going to do about February? You can use frac=1 to get the last day of the month (hat tip #Henrik):
> as.Date(as.yearmon(s),frac=1)
[1] "2017-09-30"
> as.Date(as.yearmon("February 2018"),frac=1)
[1] "2018-02-28"
> as.Date(as.yearmon("February 2020"),frac=1)
[1] "2020-02-29"
Related
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)
I am running into some date issues when working with Dates in R.
Here's my situation.
I'm working on a dataset with a column date (ProjectDate) having the following values
class(Dataset$ProjectDate)
"character"
head(Dataset$ProjectDate)
"End July 2014" "End August 2014" "End September 2014" "End October 2014"
I would like to convert it to "%M %Y" format
How can I do that ?
Thanks
You should think of using 2 step process. First remove the End part from the ProjectDate using sub.
Now you can apply yearmon from zoo library to convert to month year date format.
library(zoo)
as.yearmon(sub("^End ", "", df$ProjectDate), "%b %Y")
#[1] "Aug 2014" "Sep 2014"
Try the following.
First, the data.
x <- scan(what = character(),
text = '"End July 2014" "End August 2014"
"End September 2014" "End October 2014"')
Now the conversion to dates. Note that your dates do not have a day, so I replace "End" by day "1".
as.Date(sub("^[[:alpha:]]+", "1", x), "%d %B %Y")
#[1] "2014-07-01" "2014-08-01" "2014-09-01" "2014-10-01"
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,
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"
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