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")
Related
I wanted to know what lubridate function can be used to convert these strings to date format.
using as_date in the above string is giving warning:
Warning message:
All formats failed to parse. No formats found
However, I am able to convert a string like this: "2020 Apr 10 11:22:23" using the as_datetime function.
With lubridate, it is just the order of day, month, year that matters. If we have multiple formats, use parse_date_time
library(lubridate)
parse_date_time(date1, orders = c('dmy', 'mdy'))
[1] "2020-04-21 UTC" "2020-04-21 UTC"
data
date1 <- c("21 Apr 2020", "April 21, 2020")
This is non-lubridate, but: if you don't know the order (d-m-y vs m-d-y vs y-m-d) in advance, or if it could be mixed within a single vector, you could try the anytime package:
anytime::anydate(c("21 Apr 2020","April 21, 2020"))
## [1] "2020-04-21" "2020-04-21"
(Apparently lubridate::parse_date_time() can handle mixed formats as well: it seems to allow slightly more control of which formats are checked for.)
It was this simple. Thank you guys :)
library(lubridate)
a <- "21 Apr 2020"
day1 <- dmy(a)
b <- "April 21, 2020"
day2 <- mdy(b)
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"
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"
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"