Converting character to date in lubridate - r

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.

Related

Formate numeric (decimal) value into dates

I have a df with a column of the formate df$date = (1800.01, 1800.02, 1800.03) and so on.
And I can't figure out how to convert these numbers into proper monthly dates.
I tried the function date_decimal from the lubridate package, but that does not work for how my dates are formated.
Any ideas?
Probably the best way is to convert to a character first, then use one of the standard conversion functions. e.g.:
lubridate::ym(as.character(df$date))

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"

I can't convert an unknown column in to a date column and then change format to dd-mm-yyyy

I know it seems that this may be a repeated question, but I have tried other solutions and still cannot get it to work. I have uploaded a .csv file into r. I have done a small amount of house cleaning but ultimately I would like to convert a column from '"POSIXct" "POSIXt"' to a 'date' column type, and a 'character' column to a 'numeric'. For the latter column (change) I have decimals and --- entries, I converted the --- to NA, but fail to convert it to a 'numeric' afterwards.
df$value <- as.numeric(as.character(df$value))
I first used:
df$date <- dmy_hm(df$time_stamp, tz = "Europe/London")
to create a new date variable / column. But this did not give 'date' as a column type. I then tried using:
df$date <- as.Date(df$date)
but this did not work. Once I have converted to 'date' I need to convert the format from yyyy-mm-dd hh:mm:ss to dd/mm/yyyy.
Any help with will greatly received.
lubridate package can be wacky sometimes. Can you share head of you .csv data? you might have confused with dmy_hms with myd_hms or ymd_hms formats. Try using anytime package.
anytime::anytime(df$time_stamp)

Formatting and converting column into datetime format in R?

I am fairly new to R and need help with applying operations to an entire column in a dataframe. Imagine a few values of the date_time column in the df look like this:
date_time
2017-05-01T00:00:00.000Z
2017-05-01T10:00:00.000Z
2017-05-01T20:00:00.000Z
...
Currently date_time is of type factor. If everything was formatted nicely, I think I want to do something similar to what I have below to convert it to DateTime (based off of what I've been seeing online):
df$date_time <- strptime(x = as.character(df$date_time), format = "%Y-%m-%d %H:%M:%S")
Does this look correct?
Assuming the code above is correct for converting the factor into DateTime, we still need to do some formatting for that to work. In order to use the code above, I have to get rid of the T that separates the date and time and replace that with ' ', and cut off the .000Z at the end. How can I do this for the entire column?
Thanks!

Specify output of as.Date without the year (R)

I am trying to convert a vector of factors into a vector of dates. The data is formatted as month/date (e.g. 5/20, 4/13, 11/11). I want to retain the format but need to change the data type from factor to date.
df$date <- as.Date(df$date, format = '%m/%d')
Is what I have at the moment. What is returned is 2017-5-20 or 2017-4-13 for example. My question is, is there a way to have as.Date not return the year? Additionally, is there a way to do this in lubridate that may be more efficient? I would like my output to return 5-20 or 4-13. All examples I find online always include the year and seem to leave my question unanswered.

Resources