Skip to content

Instantly share code, notes, and snippets.

@gabrielburcea
Last active July 26, 2018 14:16
Show Gist options
  • Save gabrielburcea/35cadcbf40cfdc55b3e969621cbbd7a7 to your computer and use it in GitHub Desktop.
Save gabrielburcea/35cadcbf40cfdc55b3e969621cbbd7a7 to your computer and use it in GitHub Desktop.
A&E - arrivals - March to April, 27th, 2013
###################################################
# A&E - arrivals - March to April, 27th, 2013 ##
###################################################
#######################
#reading the data #####
#######################
data <- read.csv("df.csv", header = TRUE)
##################################
# Importing the libraries I need##
##################################
library(lubridate)
library(tidyverse)
##################################
##################################
########################################
########################################
# Subseting the data I am interested in#
########################################
head(data, 10)
data$X <- NULL
#######################################################
# Code that converts all the columns names to lowercase
df_tolower <- function(x) {
colnames(x) <- tolower(colnames(x))
x
}
#####################################################
df <- df_tolower(data)
################################################################################################################
# subseting data set
################################################################################################################
dt_rmds<- df[c("pat_code","start_datetime", "end_datetime", "episode.order", "spell.number")]
################
################
colname_ct <- function(df, colname){
df[,colname] = as.POSIXct(df[,colname])
df
}
################################################
# applying function to change col to POSIxCT ###
################################################
################################################
dt_rmds_ct <- dt_rmds %>%
colname_ct("start_datetime") %>%
colname_ct("end_datetime")
#################################
# Subseting the data
#################################
dt_hocc_month <- subset(dt_rmds_ct, (start_datetime >= "2013-03-01 00:00:00" & start_datetime <= "2013-04-27 23:59:00" | end_datetime >= "2013-03-01 00:00:00" & end_datetime <= "2013-04-27 23:59:00"))
str(dt_hocc_month)
###########################################################################
# Finding the hour from Admission Date for each patient ######
###########################################################################
dt_adm_count <- dt_hocc_month %>%
mutate(
Time = ymd_hms(start_datetime),
Hour_admission = hour(start_datetime),
Day = day(start_datetime),
Weekday = wday(start_datetime),
Month = month(start_datetime)
) %>%
dplyr::count(Hour_admission, Day, Weekday, Month)
###############################
# Count the admissions ########
###############################
admission_dt_avg_plot <- dt_adm_count %>%
group_by(Hour_admission) %>%
summarise(Avg = mean(n)) %>%
na.omit()
###############################
# Ploting
###############################
ggplot(admission_dt_avg_plot, aes(y = Avg, x = Hour_admission, group = Avg, fill = Avg))+
scale_x_continuous(breaks = 0:23) +
#geom_line(size = 1) + # does not look nice at all
geom_bar(stat = "identity", width=0.7, fill="steelblue") +
scale_y_continuous(expand = c(0, 0), limits = c(0, 30)) +
labs(title = "Arrivals in Hospital, 1st of March to 27th of April 2013",
subtitle = "Average ED of daily hospital arrivals by hour of the day.
Note: results are intended for management information only",
y = "Average- Hospital Arrivals", x = "Hours of the day", caption = "Source: CLAHRC NIHR") +
theme(axis.title.y = element_text(margin = margin(t = 0, r = 21, b = 0, l = 0)),
plot.title = element_text(size = 12, face = "bold"),
plot.subtitle = element_text(size = 10)) +
theme_bw()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment