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)
Related
This question already has answers here:
Change Date print format from yyyy-mm-dd to dd-mm-yyyy
(2 answers)
Closed 1 year ago.
I have a character column read in from a csv in the format dd/mm/yyyy, with some blank rows being read in as na. I wish to convert this to a date column and maintain the dd/mm/yyyy format.
df$Date <- strftime(df$Date, "%d/%m/%Y")
The above line causes the date to be gibberish. An example output is 20/06/0029 I think this is the first two digits of the year/mm/00dd
df<- transform(df, Date = chron(Start Date, format = "d/m/Y"))
The above using the chron package gives me dd/mm/yy but I can't adjust it to be dd/mm/yyyy
df$Date <- as.Date(df$Date, format = "%d/%m/%Y")
The above line returns yyyy-mm-dd
What am I doing wrong?
That should work:
# Source string
today <- "02/03/2021"
# Converted
today_as_date <- as.Date(today, format = "%d/%m/%Y")
# Convert back to character
strftime(today_as_date, "%d/%m/%Y")
This question already has answers here:
Convert date-time string to class Date
(4 answers)
Closed 2 years ago.
Hi I have a data frame (df) that looks like:
Date
Jun.1
Jan.1
Jul.3
Mar.1
It's a character type.
Trying to use the lubridate package to convert to a date type. However, the closest thing I can find is mdy which converts when the format is e.g. January 31st, 2017.
So, I tried to replace all the '.' to '' using gsub('.', '', df$date), but this just returns NAs.
Any advice?
We can specify the truncated in mdy to make up for the missing component. It will return the current 'year' as it is missing
df$Date <- mdy(df$Date, truncated = 2)
df$Date
#[1] "2020-06-01" "2020-01-01" "2020-07-03" "2020-03-01"
data
df <- structure(list(Date = c("Jun.1", "Jan.1", "Jul.3", "Mar.1")),
class = "data.frame", row.names = c(NA,
-4L))
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 "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
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"