I have a data frame with two text columns which are essentially time-stamps columns namely BEFORE and AFTER and the format is 12/29/2016 4:29:00 PM. I want to compare if the gap between the BEFORE and AFTER time for each row is more than 5 minutes or not. Which package in R will allow subtraction between time stamp?
No need for extra packages, using base R you can achieve the date time comparison.
First coerce your character to a valid date time structure. Here I am using POSIXct:
d1= as.POSIXct("12/29/2016 4:29:00 PM",format="%m/%d/%Y %H:%M:%S")
d2= as.POSIXct("12/30/2016 5:29:00 PM",format="%m/%d/%Y %H:%M:%S")
Then to get the difference in minutes you can use difftime:
difftime(d1,d2,units="mins")
Note the all those functions are vectorized So the same code will work with vectors.
manage PM/AM
to take care of PM/AM we should add %p to the format that should be used in conjonction with %I not %H:
d1= as.POSIXct("12/28/2016 11:53:00 AM",
format="%m/%d/%Y %I:%M:%S %p")
d2= as.POSIXct("12/28/2016 12:03:00 PM",
format="%m/%d/%Y %I:%M:%S %p")
difftime(d2,d1,units="mins")
## Time difference of 10 mins
for more help about date/time format you can read ?strptime.
Related
I currently have data as such:
data
I wish to change the 'date' column to date type. (it is now in character).
I have tried the code below but it gives me 'NA' as the result.
as.Date(data$Date, format = "%a %d-%m-%Y %I:%M %p")
Am I making a mistake in the way I am formatting the date to suit the format in my data?
Unfortunately, we don't have your data (we have a picture of your data, but the only way to use that is to transcribe it manually - best to include data as text in your questions).
Anyway, using a brief example in the same format:
dates <- c("Wed 25-Apr-2018 3:20 PM", "Thu 10-Mar-2022 10:53 AM")
We can get the dates by doing:
as.Date(dates, "%a %d-%b-%Y %I:%M %p")
#> [1] "2018-04-25" "2022-03-10"
Note though that this does not preserve the time, and to do this you probably want to use R's built in date-time format, POSIXct. We can get this with:
strptime(dates, "%a %d-%b-%Y %I:%M %p")
#> [1] "2018-04-25 15:20:00 BST" "2022-03-10 10:53:00 GMT"
I think the main problem was that you were using %m for the month, but this only parses months in decimal number format. You need %b for text-abbreviations of months.
For my analysis, I need to add days of the week to the dates in Rstudio. My data starts from the first day of January ( 2019-01-01 00:00:00) and time steps are 5 minutes therefore, the second term is "2019-01-01 00:05:00 to the last day of the year. Unfortunately, some rows are missing and for example, in one case next reading after 2019-05-01 10:05:00 can be 2019-05-17 23:05:00. How can I assign days of the week to my dates?
How's this work? First, we convert the date into the magic R format, and then extract it back in the format we want.
posix <- strptime(x = "2019-01-01 00:00:00",
format = "%Y-%m-%d %H:%M:%S")
strftime(posix, format = "%Y-%m-%d %H:%M:%S %A")
Here, we're essentially exporting it in the exact same format, plus the %A operator that corresponds to the weekday.
The date July, 1, 2016 1:15pm and 43 seconds is given to me as the string 160701131543.
I have an entire column in my data frame of this date time. How should I go about parsing this column into usable data.
You can use the as.POSIXct function and specify the format, in your case the format is year, month, day, hour, minute, second. Read more about formatting date and time data on the ?strptime help page.
as.POSIXct("160701131543", format = "%y%m%d%H%M%S")
[1] "2016-07-01 13:15:43 EDT"
The timezone can be changed with the 'tz' parameter.
Here is another option with lubridate. The default tz is "UTC". It can be changed by specifying tz
library(lubridate)
ymd_hms("160701131543")
#[1] "2016-07-01 13:15:43 UTC"
I have to do a difference between dates and return the number of days.
the format of the dates is as follows:
12/9/2011 12:00:00 AM
Does anyone know how to perform the difference without using lubridate?
We can use asPOSIXct to convert to DateTime
v1 <- as.POSIXct("12/9/2011 12:00:00 AM", format = "%d/%m/%Y %I:%M:%S %p")
If we need only Date
as.Date(v1)
Convert datetime column in date format
df$DATETIME<-as.Date(df$DATETIME)
I would like to ask the R gurus to comment of the following:
as.POSIXct("05/11/1998 09:35", "%m/%d/%Y %H:%M",tz="EST") - as.POSIXct("1998-05-11 09:35:00 EST")
Time difference of 1 hours
Shouldn't it be zero since dates are the same?
Thanks.
According to ?strptime (which ?as.POSIXct points to) the format= argument should be
A character string. The default for the ‘format’ methods is
‘"%Y-%m-%d %H:%M:%S"’ if any element has a time component
which is not midnight, and ‘"%Y-%m-%d"’ otherwise. If
‘options("digits.secs")’ is set, up to the specified number
of digits will be printed for seconds.
The time "1998-05-11 09:35:00 EST" has a format of "%Y-%m-%d %H:%M:%S %Z". However %Z can only be used for output (see ?strptime)
If you provide the tz= argument to the second call, it will work as expected
> as.POSIXct("05/11/1998 09:35", "%m/%d/%Y %H:%M",tz="EST") - as.POSIXct("1998-05-11 09:35:00 EST", tz="EST")
Time difference of 0 secs
It is worth noting that
'EST' is a time zone used in Canada _without_ daylight saving time, and not
‘EST5EDT’ nor (Australian) Eastern Standard Time.)
(see ?timezone)