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"
Related
I am having trouble converting timestamps in character class. I am trying to use following function
df$Time <- strptime(df$Time, format = "%H:%M:%S")
Then problem I have is that my Time column (currently class Character) has values listed like this 1:00:05 or 55:42. There are no dates in the column.
While the 55:45 is in mm:ss, the 1:00:05 is in hh:mm:ss. Could this be the problem? I can't think of anything else. When I run the above function, I keep getting NA for the whole column. How can I resolve this.
Thanks for any help. I have been stuck and have tried so many different functions such as hms from lubridate and as.POSIXct
Here is sample dataset
df <- data.frame(Time = c("1:00:05", "55:45","34:33","1:12:35"))
df$Time <- as.character(df$Time)
head(df)
We may use parse_date_time
library(lubridate)
parse_date_time(df$Time, c("HMS", "MS"))
I have a character column of a df looking like:
a <- "6/10/21 19:34"
where the order is m/d/Y H:m.
I can't figure out why I am not able to convert it into datetime object.
I tried with:
mdy_hms(a,tz=Sys.timezone())
But I can't find a viable solution.
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
Just a brief (and hopefully simple!) question;
I have a column of dates such as 08/14 and 08/15. However the column class is character, and I was wondering if it is possible (and if so how) to convert this into a date class? I've tried lots of different ways and it either does nothing, or returns N/A values for the whole column. Hopefully there should be a simple line of code to fix this that I haven't come across yet?
Any help much appreciated!!
One option is to create a day by pasteing and then use as.Date because date also needs day info
as.Date(paste0(c("08/14", "08/15"), "/01"), format = "%m/%y/%d")
#[1] "2014-08-01" "2015-08-01"
Or make use of as.yearmon from zoo
library(zoo)
as.Date(as.yearmon(c("08/14", "08/15"), "%m/%y"))
library(dplyr)
dates <- c("08/14", "10/13", "12/09") # year-month
datesWithYear <- paste(date, "/01", sep = "") %>%
as.Date()
If you want to use the year-month combination as a factor for cohort analysis of something similar you can always use as.yearmon() from the zoo package. tsibble and lubridate also have interesting functions for dealing with dates and time.
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")