Created
July 4, 2018 09:27
-
-
Save elizabethriddle/dabdc384719a589b2c5f2e4bb302e550 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Import data and transform to unix epoch | |
occupancy_df <- read_csv("~/Documents/MRes/occupancy_df.csv") | |
occupancy_df$spell.start_sec<-as.numeric(as.POSIXct(occupancy_df$spell.start)) | |
occupancy_df$spell.end_sec<-as.numeric(as.POSIXct(occupancy_df$spell.end)) | |
# Filter to only have medical | |
occupancy_df_medical <- occupancy_df[occupancy_df$Specialty.Group=='Medical',] | |
occupancy_df_medical <- occupancy_df_medical[!is.na(occupancy_df_medical$Specialty.Group),] | |
# Remove entries of same spell | |
unik<-!duplicated(as.vector(occupancy_df_medical$spell.number)) | |
occupancy_df_medical_spell <- occupancy_df_medical[unik,] | |
# | |
start_split<-split(occupancy_df_medical$spell.start_sec,occupancy_df_medical$spell.number) | |
end_split<-split(occupancy_df_medical$spell.end_sec,occupancy_df_medical$spell.number) | |
occupancy_df_medical_spell$spell.start_sec<-sapply(start_split,min) | |
occupancy_df_medical_spell$spell.end_sec<-sapply(end_split,max) | |
# Get hour of spell | |
occupancy_df_medical_spell$spell.start_1hr<-occupancy_df_medical_spell$spell.start_sec%/%3600 | |
occupancy_df_medical_spell$spell.end_1hr<-occupancy_df_medical_spell$spell.end_sec%/%3600 | |
# Code to calculate occupants per hour | |
list_1hr<-unique(occupancy_df_medical_spell$spell.start_1hr) | |
number_occupants_1hr<-data.frame(day=list_1hr*3600) | |
occupants<-c() | |
for(i in list_1hr){ | |
occupants_1hr <- sum((occupancy_df_medical_spell$spell.start_1hr<=i & occupancy_df_medical_spell$spell.end_1hr>=i)) | |
occupants<-c(occupants,occupants_1hr) | |
} | |
number_occupants_1hr$occupants <- occupants |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment