I have a dataset with the Date column as:
Date
-------------
Aug. 21, 2022
Aug. 19, 2022
Aug. 18, 2022
...
Aug. 22, 2017
And I need the format to be in typical Date syntax i.e %d-%m-%y. However when I run the code
ftse$Date <- as.Date('Aug. 21, 2022', format = '%b-%d-%y')
ftse$Date <- format(ftse$Date, '%d-%m-%Y')
I get a column of NAs. My guess would be that the mix of a hyphen and full stop don't agree with the format function. Does anyone have any idea how to change?
Many thanks in advance.
Your format is slightly different than the one you indicate. Since you have a dot and a comma, you have to include them in the format string. As mentioned in the comments, you also need to set %Y (4-digit year) instead of %y (2-digit year).
as.Date('Aug. 21, 2022', format = '%b. %d, %Y')
#[1] "2022-08-21"
format(as.Date('Aug. 21, 2022', format = '%b. %d, %Y'), '%d-%m-%Y')
#[1] "21-08-2022"
completing akrun's answer:
install.packages("parsedate")
library(parsedate)
ftse$Date<-as.Date(parse_date(ftse$Date))
Related
This question already has an answer here:
R formatting a date from a character mmm dd, yyyy to class date [duplicate]
(1 answer)
Closed 2 years ago.
I have a column full of dates in format "mmm dd,yyyy".
They look like these:
"Dec 21, 2015"
"Aug 23, 2018" etc.
I tried to convert those dates to a proper date format for further data processing by doing as.Date("Dec 21, 2015") or as.Date(as.character("Dec 21, 2015"))but neither worked.
Here is the error message:
Error in charToDate(x) : character string is not in a standard unambiguous format
I am wondering what is a good way to properly convert such nonstandard data format into proper data format and class "YYYY-MM-DD" (e.g. "2015-12-21")? Should I use regex syntax to solve that?
Thanks so much for your help!
Using lubridate:
library(dplyr)
library(lubridate)
df <- data.frame(date = c('Dec 21, 2015','Aug 23, 2018'))
df
date
1 Dec 21, 2015
2 Aug 23, 2018
df %>% mutate(date1 = mdy(df$date))
date date1
1 Dec 21, 2015 2015-12-21
2 Aug 23, 2018 2018-08-23
Using base:
x <- "Dec 21, 2015"
as.Date(x, "%b %d, %y")
#> [1] "2020-12-21"
Created on 2020-11-16 by the reprex package (v0.3.0)
Depending on what your date variable is named, it can look something like this:
df$date <- as.Date(df$date, "%b %d, %y")
An option with anydate from anytime
library(anytime)
df$date <- anydate(df$date)
df$date
#[1] "2015-12-21" "2018-08-23"
data
df <- data.frame(date = c('Dec 21, 2015','Aug 23, 2018'))
I have a character string "April 20, 2020 - April 24, 2020" and would like to convert it to two separate strings. There are two problems here: (1) recognizing that the narrative "April" is the 4th month, and (2) that there are two different dates in this string.
I have looked at the following, plus others, but don't see my answer:
[extract dates from date range][1],
[select a range of dates][2],
[convert dates to range][3]
I've also studied "parse" but that doesn't seem to be the answer.
Would something like this work ?
string <- "April 20, 2020 - April 24, 2020"
dates <- as.Date(strsplit(string, ' - ')[[1]], '%B %d, %Y')
dates
#[1] "2020-04-20" "2020-04-24"
Or with lubridate::mdy if you don't want to remember the formats.
dates <- lubridate::mdy(strsplit(string, ' - ')[[1]])
Note that this is locale-dependent, your locale should be English.
We can use anydate
library(anytime)
anydate(strsplit(string, "\\s*-\\s*")[[1]])
#[1] "2020-04-20" "2020-04-24"
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 a dataframe, and one of the columns are dates that are stored in the format "month_name day, year" (e.g. "December 25, 2016"). How do I convert these to date objects? Upon searching, I came across the following code:
df$date <- as.Date(df$date, format="%m/%d/%Y")
But upon viewing the data, the date column is NA.
You can try:
as.Date("December 25, 2016", format = '%B %d, %Y')
#[1] "2016-12-25"
You need to specify the format argument in as.Date based on how your dates look like. The full details of all the options are in ?strptime.
library(lubridate)
mdy("December 25, 2016")
#[1] "2016-12-25"
Or you can use the all inclusive anytime package which handles almost all the cases.
library(anytime)
anytime("December 25, 2016")
#[1] "2016-12-25 MYT"
I have the data in character format as " Mar 26, 2015 7:46:22 PM CDT " I have convert this into a Date format and also fetch the month, year and day separately.
Also is it possible to conevert it into a integer format.
Please advice.
You have to specify the format of the input string. Try:
as.POSIXct("Mar 26, 2015 7:46:22 PM CDT", format="%b %d, %Y %I:%M:%S %p")