Existing topic?
Yes
What is the topic?
SMRA
Context for the code
Quick and easy way to calculate the number or average number of days between two or more hospital admissions.
Code / Script
# Loading required packages
library(dplyr)
vector_1 <- data_frame %>%
filter(upi_number == "ABC123DOREMI") %>% # Filters for specific user. Could also be a high-resource ICD-10 code
mutate(date_lag = lag(discharge_date), # Creates a new variable with the discharge data lagged by 1 index position
admission_diff = as.numeric(admission_date - date_lag), # Determines the date difference between an initial discharge and subsequent admission
.after = discharge_date) %>% # Positions both variables (optional)
pull(admission_diff) # Pulls vector only. These should be series of discrete values denoting no. of days.
# From here, you can calculate the mean:
mean <- vector_1 %>%
mean(na.rm = T) %>% # First lagged entry will be void. This calculates the mean with the NA values removed
round() # Rounds mean calculation to a single integer value
# And min/max values:
## For max value
max <- vector_1 %>%
arrange(desc()) %>%
first()
## For min value
min <- vector_1 %>%
arrange(desc()) %>% # NB: Could also use arrange() %>% first() here.
last()
Existing topic?
Yes
What is the topic?
SMRA
Context for the code
Quick and easy way to calculate the number or average number of days between two or more hospital admissions.
Code / Script