how to convert factor into DateTime in R? - r

This question might be simple for some of you but bear with me since I'm a beginner in R.
I have a dataframe that has a factor column (called time) containing DateTime data as the following:
time
01/01/2011 00:10
02/01/2011 03:00
03/01/2011 05:00
04/01/2011 10:03
I want to convert this column into DateTime column in R. I searched and tried some functions but it gives me 'NA' results. The following functions are those I tried:
> dataframe1$datetime <- as.POSIXlt(as.character(dataframe1$time), format="%d/%m/%Y %H:%M")
> dataframe1$datetime <- as.POSIXlt(strptime(dataframe1$time), format="%d/%m/%Y %H:%M")
> dataframe1$datetime <- as.POSIXlt(dataframe1$time, format="%d/%m/%Y %H:%M")
> dataframe1$datetime <- as.chron(dataframe1$time, "%d/%m/%Y %H:%M")
I don't know what else to try. I want ideally to add three columns namely datetime, date, and time.

Try:
dataframe1$datetime <- strptime(x = as.character(dataframe1$datetime),
format = "%d/%m/%Y %H:%M")

Probably the easiest thing to do is use the lubridate packages which has a large number of functions for date manipulation. The following will convert your time into a POSIXct object:
library(lubridate)
mdy_hm( as.character(dataframe1$time) )
See ?mdy to see the variety of date parsing functions.
For a slightly more verbose version that does not rely on lubridate
strptime(x = as.character( dataframe1$datetime ), format = "%d/%m/%Y %H:%M")

Related

convert date with time formats [R]

how can i convert a date from a format like yyyymmdd H:M to yyyy-mm-dd H:M, basicaly from 20200101 00:00 to 2020-01-01 00:00. i have tried multiple as.Date formats and cant obtain the result i want
example of what i have :
dates <- c("20200101 00:00", "20200101 01:00")
want <- as.Date(have, format="%Y%m%d %H:%M")
the output:
> want<- as.Date(dates, format="%Y%m%d %H:%M")
> want
[1] "2020-03-01" "2020-03-01"
There's two pieces here. One is converting to date time class, such as POSIXt. Then there is how this object is printed. Under the hood it's all represented the same, but you can control how it's displayed.
The format argument in any of the conversion functions (as.Date or as_datetime) is describing how to parse the string representation into the components of a data time object (e.g. where in the string to find the minutes). You need to then use something like format or strftime to then control how the values are printed/displayed.
Below is what I think you're aiming for:
dates_as_strings <- c("20200101 00:00", "20200101 01:00")
dates_as_datetime_objs <- lubridate::as_datetime(dates_as_strings, format="%Y%m%d %H:%M")
strftime(dates_as_datetime_objs, "%Y-%m-%d %H:%M", tz = "UTC")
#> [1] "2020-01-01 00:00" "2020-01-01 01:00"
Created on 2021-05-21 by the reprex package (v1.0.0)

Change format of column in R

In my table I have a timestamp column in which i want to change the format. I already tried multiple things but it doesnt work. I also can not find examples to change the format in the whole column. First i converted the column to POSIXct then Ive tried to adjsut the format:
#timestamp as POSIXct and lane as numeric
Flows_ALM_2019 %>%
mutate(timestamp = as.POSIXct(timestamp),lane = as.numeric(lane)) -> Flows_ALM_2019
#remove seconds from time in timestamp column
df <- strftime(timestamp, format= "%Y-%m-%d %H:%m")
after trying:
df_2019_tsb$timestamp <- format(df_2019_tsb$timestamp,format= "%Y-%m-%d %H:%m")
This is what I had:
and now it changed to:
What am i doing wrong?
You can use format :
Flows_ALM_2019$timestamp <- format(Flows_ALM_2019$timestamp, "%Y-%m-%d %H:%m")
Or with strftime.
Flows_ALM_2019$timestamp <- strftime(Flows_ALM_2019$timestamp, "%Y-%m-%d %H:%m")
This is assuming that timestamp column is of class POSIXct in your data.

Remove timezone during POSIXlt Conversion in R

I have a column in my dataframe as datetime (factor) with the values as "15-10-2017 16:41:00".
I wanted this data to be converted as "2017-10-15 16:41:00".
When i try to convert this, I'm getting the timezone also as output.
I tried using tz="", usetz=F but no use.
Any suggestions ?
Code:
as.POSIXlt("15-10-2017 16:41:00",format = "%d-%m-%Y %H:%M:%S")
[1] "2017-10-15 16:41:00 IST"
From the help page of as.POSIXlt:
"" is the current time zone
which is the default.
That's why it does not work. You could remove the timezone information this way, and it will not show while printing:
my_datetime <- as.POSIXlt("15-10-2017 16:41:00",format = "%d-%m-%Y %H:%M:%S")
my_datetime$zone <- NULL
my_datetime
but I don't understand why you would want to do that. You should convert to GMT if you don't want to worry about the timezone. Also lubridate package has a nice force_tz function if you have to force some specific timezones.
If you are ok storing the datetime as a character instead of as a POSIXlt, then you can use strftime():
my_datetime <- as.POSIXlt("15-10-2017 16:41:00",format = "%d-%m-%Y %H:%M:%S")
strftime(my_datetime)
I do it like this:
strip.tz <- function(dt) {
fmt <- "%Y-%m-%d %H:%M:%S"
strptime(strftime(dt, format = fmt, tz=""), format = fmt, tz="UTC")
}
and you would use it like this:
my_datetime <- as.POSIXct("15-10-2017 16:41:00",format = "%d-%m-%Y %H:%M:%S")
strip.tz(my_datetime)

Change Format of Date Column

I need to turn one date format into another with RStudio, since for lubridate and other date related functions a standard unambiguous format is needed for further work. I've included a few examples and informations below:
Example-Dataset:
Function,HiredDate,FiredDate
Waitress,16-06-01 12:40:02,16-06-13 11:43:12
Chef,16-04-17 15:00:59,16-04-18 15:00:59
Current Date Format (POSIXlt) of HiredDate and FiredDate:
"%y-%m-%d %H:%M:%S"
What I want the Date Format of HireDate and FiredDate to be:
"%Y-%m-%d %H:%M:%S" / 2016-06-01 12:40:02
or
"%Y/%m/%d %H:%M:%S" / 2016/06/01 12:40:02
In principle, you can convert date and time for example using the strftime function:
d <- "2016-06-01 12:40:02"
strftime(d, format="%Y/%m/%d %H:%M:%S")
[1] "2016/06/01 12:40:02"
In your case, the year is causing trouble:
d <- "16-06-01 12:40:02"
strftime(d, format="%Y/%m/%d %H:%M:%S")
[1] "0016/06/01 12:40:02"
As Dave2e suggested, the two digit year can be read by %y:
strftime(d, format="%y/%m/%d %H:%M:%S")
[1] "16/06/01 12:40:02"
Assuming that your data comes from the 20st and 21st century, you can paste a 19 or 20 in front of the HireDate and FireDate:
current <- 16
prefixHire <- ifelse(substr(data$HireDate, 1, 2)<=currentYear,20,19)
prefixFire <- ifelse(substr(data$FireDate, 1, 2)<=currentYear,20,19)
data$HireDate = paste(prefixHire, data$HireDate, sep="")
data$FireDate = paste(prefixFire, data$FireDate, sep="")
The code generates a prefix by assuming that any date from a year greater than the current ('16) is actually from the 20th century. The prefix is then pasted to HireDate and FireDate.

How To Split a Date & Time Observation

I am just learning R and have come up against this.
I have the below time series observations,
10/08/2015 02:31:04.450
I want to split the date and the time to separate columns.
Do i need need to round the Milliseconds in time? if so how.
I have been looking at, data table, lubridate to try and figure it out. I looked at XTS but that seems to be more orientated to aggregation of dates.
Are they any existing packages in R that allows for this splitting? and what sort of argument would I use.
Any help would be much appreciated.
Using data.table it is very straight forward:
require(data.table)
x <- "10/08/2015 02:31:04.450"
IDateTime(strptime(x, "%d/%m/%Y %H:%M:%OS"))
gives you the following data.table
idate itime
1: 2015-08-10 02:31:04
x <- "10/08/2015 02:31:04.450"
temp <- strptime(x, "%d/%m/%Y %H:%M:%OS")
format(temp,"%H:%M:%S")
#[1] "02:31:04"
as.Date(temp)
#[1] "2015-08-10"
If you do not need the time part in character form you can add few steps
x <- "10/08/2015 02:31:04.450"
temp <- strptime(x, "%d/%m/%Y %H:%M:%OS")
library(chron)
chron(times = format(temp,"%H:%M:%S"))
#[1] 02:31:04
class(chron(times = format(temp,"%H:%M:%S")))
#[1] "times"
as.Date(temp)
# [1] "2015-08-10"
string_timeStamp = "10/08/2015 02:31:04.450"
parsed_timeStamp = strptime(string_timeStamp, "%d/%m/%Y %H:%M:%OS")
date_time_dataFrame = data.frame(date = cut(parsed_timeStamp, breaks = "days"),
time = format(parsed_timeStamp, "%H:%M:%OS" ))
For more formatting options, check ?strptime

Resources