Different Date Formats in R [duplicate] - r

This question already has answers here:
Convert dd/mm/yy and dd/mm/yyyy to Dates
(6 answers)
Closed 4 years ago.
In my R data set there is a data$date variable, made of two different writting : some are dd-mmm-yy (ex. "14-nov-17") and others are ddMMMyyyy (ex. "14APR2016").
Here I'm stuck. How can I get all of them to date format ?
Thank you

An option would be parse_date_time from lubridate which can take multiple Date formats
library(lubridate)
parse_date_time(v1, c("%d-%b-%y", "%d%b%Y"))
#[1] "2017-11-14 UTC" "2016-04-14 UTC"
Or with anydate from anytime. But, applying anydate, check whether all the formats are already present with
library(anytime)
getFormats()
If some formats are missing, add it with addFormats
addFormats("%d-%b-%y")
and then apply the anydate on the column/vector of dates
anydate(v1)
#[1] "2017-11-14" "2016-04-14"
data
v1 <- c("14-nov-17", "14APR2016")

Another option, if you want to use base R and like regular expressions:
data$date <- as.Date(sub('(\\d{2})(\\w{3})(\\d{2})(\\d{2})', '\\1-\\2-\\4', data$date),
format = "%d-%b-%y")

Related

Make date readable by R

I have a column in a dataset right now formatted for example as "Aug-19" which represents 08/01/2019 in mm/dd/yyyy format. How do I convert this into a format that can be read by R?
You can use lubridate package.
dates <- c("Aug-19", "Sep-19")
dates_myd <- paste0(dates, "-01")
lubridate::myd(dates_myd)
With lubridate we can also do
myd(dates, truncated = 1)
#[1] "2019-08-01" "2019-09-01"
data
dates <- c("Aug-19", "Sep-19")

Convert an entire column from character to date class in R [duplicate]

This question already has answers here:
Convert date-time string to class Date
(4 answers)
Closed 2 years ago.
I am using this dataset and I am looking to convert the ArrestDate column from character in to dates so that I can work with dates for analysis.
I've first tried using mutate:
Date <- mutate(crime, ArrestDate = as.Date(ArrestDate, format= "%d.%m.%Y"))
however when I do this the entire ArrestDate column is changed to NAs.
Secondly I tried using strptime but for some reason it converts some dates fine and others to NA:
Date <-strptime(paste(crime$ArrestDate, sep=" "),"%d/%m/%Y")
crime2 <- cbind(Date, crime)
Anyone able to tell me what I am doing wrong or alternatively provide a better approach for this?
Thanks.
The lubridate package offers some very useful functions to transform strings into dates. In this case you can use mdy() since the format is '%m/%d/%Y' (as can be derived from the first record which is '12/31/2019').
library(dplyr)
library(lubridate)
crime %>%
mutate(ArrestDate = mdy(ArrestDate))
Replacing the '.' with '/' works from your first example in the format:
Date <- mutate(crime, ArrestDate = as.Date(ArrestDate, format= "%m/%d/%Y"))
class(Date$ArrestDate)
[1] "Date"

as.Date() function for %m/%Y (e.g. 04/2020) format [duplicate]

This question already has answers here:
Convert "Jan.2008" to date variable
(2 answers)
Closed 2 years ago.
My df has a date column with month/year dates written like 04/2020. I am trying the as.Date() function to get the right class (because it is a character now) but it only leaves me with NA output. Any help is much appreciated!
We can paste a day and then as.Date should work as 'Date' include day as well
as.Date(paste0(df$date, "/01"), "%m/%Y/%d")
Or convert to yearmon class with as.yearmon (from zoo) and wrap with as.Date
library(zoo)
as.Date(as.yearmon(df$date, "%m/%Y"))
data
df <- data.frame(date = c("04/2020", "05/2020"), stringsAsFactors = FALSE)
NOTE: From R 4.0.0, by default stringsAsFactors = FALSE

Manage multiple Date formats to POSIXct [duplicate]

This question already has answers here:
How to convert variable with mixed date formats to one format?
(3 answers)
Closed 5 years ago.
I have two dataframe with two different format of date the first is "%d/%m/%Y %H:%M:%S" and the second "%Y-%m-%d %H:%M:%S".
I want to create a function that convert to POSIXct by indicating the format.
My code:
date_func <- function(df){
colnum <- grep("DATE", colnames(df))
df[, (colnum) := lapply(.SD, dmy_hms), .SDcols = colnum]
return(df)
}
For the first format it's works but for the second I have only NA values after the conversion.
So, how can I create a function that convert to POSIXct whatever the indicated format?
Thanks for your help.
Package lubridate provides very good option to handle date/time in heterogeneous format. The parse_date_time can be used. A simple example on converting date/time in format specified in OP are:
library(lubridate)
>parse_date_time(c("01/12/2016 01:11:54", "2015-12-31 10:05:11"), c("dmY HMS", "Ymd HMS"))
# [1] "2016-12-01 01:11:54 UTC" "2015-12-31 10:05:11 UTC"

split dates column in R when there are no sperators

I have a data set with date column with following format i.e. 19700101
How can I convert it as 1970-01-01 format
I tried zoo package in R but could not work it out. Can any one help me on this
library(anytime)
anydate(19700101)

Resources