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"
Related
I have a large amount of data with 18000 rows. One of the columns is a combined date and time. In the csv file the format is "05/03/2020 05:00:00 PM". When i read the csv into R it turns into a character with the format "05-Mar-2020 17:00:00". I need R to understand this column as the date and time.
I have tried the following and it just replaces the DateTimes with NAs.
'dat' is the dataframe and 'DateTime' is the column name.
dat[['DateTime']] <- as.POSIXct(dat[['DateTime']], format = "%d%-%m-%Y %H:%M:%S")
x <- "05-Mar-2020 17:00:00"
as.POSIXct(x, format = "%d-%b-%Y %H:%M:%S")
# [1] "2020-03-05 17:00:00 CET"
use help(strptime) to see all format codes, you see that %b is used for the abbreviated month name instead of %m and that you had a redundant % sign as well.
Or you can use the lubridate package and do:
lubridate::dmy_hms(x)
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")
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"
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")
This question already has an answer here:
Converting date column in data frame
(1 answer)
Closed 6 years ago.
I have character values that are stored like this
Date <- x("05/05/15", "06/06/15")
df <- data.frame(Date)
Now I would like to convert these dates into a format: YYYY-MM-DD but doing this:
df$Date <- format(as.Date("%d/%m/%Y", df$Date))
Does not work. Any thoughts on how I can convert it?
We need to use format = "%d/%m/%y" i.e. y instead of Y as the 'year' part is only 2 digits
df$Date <- as.Date(df$Date, "%d/%m/%y")
df$Date
#[1] "2015-05-05" "2015-06-06"
Or use lubridate
library(lubridate)
dmy(df$Date)