user647764
user647764

Reputation: 47

R Change Date of timestamp based on the hour

I have a series of timestamps, and I want to make a new variable with values that have an hour value after 9am to be changed to the next day's date. If it is before 9am the date would remain the same.

2018-5-11 01:57:00  would remain 2018-05-11
2018-5-11 11:15:00  would change to 2018-5-12

Upvotes: 0

Views: 611

Answers (3)

neilfws
neilfws

Reputation: 33772

The answer depends on how your timestamps are stored, but let's assume they are of type "POSIXct" "POSIXt", as created by for example lubridate::ymd_hms.

Then we can use lubridate and dplyr. Note the use of dplyr::if_else to prevent conversion of the dates to numeric.

library(lubridate)
library(dplyr)

data.frame(dttm = ymd_hms(c("2018-5-11 11:15:00", 
                            "2018-5-11 01:57:00"))) %>% 
  mutate(hr = hour(dttm),
         dt = if_else(hr > 9, as_date(dttm + days(1)), as_date(dttm)))

Result:

                 dttm hr         dt
1 2018-05-11 11:15:00 11 2018-05-12
2 2018-05-11 01:57:00  1 2018-05-11

Upvotes: 0

12b345b6b78
12b345b6b78

Reputation: 1015

This solution answers your question, however, it might be worth it to consider writing code such that the raw data aren't mutated; engineering / extracting new variables (e.g. hour of day, "after 9 am or no" etc) for subsequent aggregation/subsetting might make development and code revision easier (especially if you're going back and forth between the end and the beginning of the script frequently).

timestamps <- as.POSIXct(c('2018-5-11 01:57:00','2018-5-11 11:15:00'), "%Y-%m-%d %H:%M:%S")
mask <- as.integer(format(timestamps, "%H")) > 9
timestamps <- as.Date(timestamps)
timestamps[mask] <- timestamps[mask] + 1
timestamps
[1] "2018-05-11" "2018-05-12"

Upvotes: 1

Corey Levinson
Corey Levinson

Reputation: 1651

library(lubridate)
library(data.table)

setDT(df)

df$timestamp_cast <- ymd_hms(df$yourtimestamp_col)
df$desired_col <- NA
df[hour(timestamp_cast) < 9, desired_col := as.Date(timestamp_cast)]
df[hour(timestamp_cast) >= 9, desired_col := as.Date(timestamp_cast)+1]

Upvotes: 0

Related Questions