convert characters to month and year ( date ) in R - r

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"

Related

Conditionally formatting a column with mutate and regex in R

I'm brand new in R and programming in general. I have a column containing a list of dates. Some are in the "01 January 2020" format, some have only month and year (ie "January 2020" only). I want to mutate them to a new field where I add a 01 in front of all the dates that are in the month year format, and then I will use lubridate to process it into dates
This is what I've tried. I'm trying to extract the first character of the Date column. If it is an upper case letter, then I will append "01" to it. I am using the tinyverse package including dplyr
df %>% mutate(new_date = ifelse(str_sub(Date, start = 1, end = 1)== "[:upper:]"), paste('01', Date, sep = ' '), new_date = Date)
I'm getting the error message "no is missing", but I thought I have included new_date = Date to keep the current formatting.
Thank you for your help!
This can be done in many ways.
base R using lookahead and backreference:
sub("(^)(?=[A-Za-z]+)", "\\101 ", date, perl = TRUE)
[1] "01 January 2020" "01 January 2020" "12 February 1999" "01 March 2033"
base R using only backreference:
sub("(^[A-Za-z]+)", "01 \\1", date, perl = TRUE)
dplyr and stringr using the same logic:
library(dplyr)
library(stringr)
data.frame(date) %>%
mutate(date = str_replace(date, "(^)(?=[A-Za-z]+)", "\\101 "))
If you do insist on using ifelse:
library(dplyr)
library(stringr)
data.frame(date) %>%
mutate(date = ifelse(str_detect(date, "^[:upper:]"),
sub("^", "01 ", date),
date))
Data:
date <- c("01 January 2020","January 2020", "12 February 1999", "March 2033")
Here is a non-regex option where we convert to Date class and format it
library(parsedate)
format(parse_date(date), '%d %B %Y')
[1] "01 January 2020" "01 January 2020" "12 February 1999" "01 March 2033"
data
date <- c("01 January 2020","January 2020", "12 February 1999", "March 2033")

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)

Converting character to date R

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"

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