I'm looking to combine two variables, day and time into a single variable consisting of hours out of 168 in a week. For example, my day variable reads "Tuesday" and the time reads "13:00". Is there a way to merge these to give me a variable consisting of the numeric value of hours out of 168 in the week? (E.g, the Tuesday 13:00 changed to 37).
Simple in theory but I can't quite figure out the best way to go about this for a bulk amount of data. I'm assuming using lubridate?
Or would it simply be easier to create a new variable and manually add this data?
CodePudding user response:
We may do a match
and then multiply by 24
and add the remaining hours
library(lubridate)
day <- "Tuesday"
hour <- "13:00"
time <- lubridate::hms(paste0(hour, ":00"))
dayind <- match(day,
c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
"Saturday", "Sunday"))
(dayind-1) * 24 hour(time) minute(time)/60
[1] 37
[1] 37
CodePudding user response:
check out strptime()
and as.POSIXct()