Im trying to calculate the occupancy rate in rooms per hour, I want to do this by checking how many minutes in an hour a room is occupied. Occupancy time is when column 'occupied' is 1 and change to 0. I have succeeded in calculating the occupancy minutes by using:
Timediff <- Test %>% arrange(Date_Time)%>% mutate(difftime= difftime(lead(Date_Time),Date_Time, units = "mins"))
And my table looks like this now:
So what I have in mind is this: To calculate the occupancy rate per hour, I'm adding all the occupied time (difftime for every 'occupied = 1') in that hour, divide it by 60, times 100%.
Example:
July 30th 2017:
Occupancy rate from 11.00 till 12.00 (11.59) = (14+1)/60*100%
12.00 till 13.00 (12.59) = (14+5)/60*100%
13.00 till 14.00 (13.59) = (11+14)/60*100%
Etc. etc.
How can I do this?
Considering that your picture represents one "room" then, you just want to use diff(yourdata$Date_Time)
and you will have a vector of occupiedtime; not occupiedtime; etc.
You can use the lag function instead of a loop.
library(dplyr)
df = df %>%
mutate(Date_Time_diff = difftime(Date_Time, lag(Date_Time), units = 'mins'))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.