Recap

  • Converted data to date format

  • Learned the power of the %>% (pipe) function

  • We wrangled data

  • Used organized data to make a table

Functions learned

as.Date()        # change the data into a date format 
%>%              # pipe function, it means "and then", 
                 # links lines of code together
select()         # allows you to subset the data you want
mutate()         # makes a new column
floor_date()     # Identifies the first date in a month 
group_by()       # groups data by a column or variable you want
summarise()      # generate summary statistics, perform math on columns
kable()          # makes a table in R
kable_styling()  # makes a really cool table in R
group_rows()     # make row headings for a kable table

Class 4 : Making Reports

Start a new markdown file

  • Title : Pediatric Neurotrauma at MRRH

  • MRRH Neurosurgery Database

  • Date : Jan 2018 - Oct 2018

Step 1: Load in Data

setwd("C:/Users/cyrus/OneDrive/Documents/R/input")
ped_one <- read_csv("ped_one.csv")
ped_two <- read_csv("ped_two.csv")

Step 2: Clean Data

one of four

ped_one2 <- ped_one %>%
  dplyr::select(
    "Admission Date",
  "Patien's Age", 
  "Gender", 
  "Actual Operation Date",
  "Outcome:"
  )

ped_one2$surg <- ifelse(is.na(ped_one2$`Actual Operation Date`), "0", "1")
ped_one2$death <- ifelse(ped_one2$`Outcome:` == "Died", "1", "0")

Step 2: Clean Data

two of four

ped_two2 <- ped_two %>%
  dplyr::select(
    "Admission Date",
  "Patien's Age", 
  "Gender", 
  "Actual Operation Date",
  "Outcome:"
  )

ped_two2$surg <- ifelse(is.na(ped_two2$`Actual Operation Date`), "0", "1")
ped_two2$death <- ifelse(ped_two2$`Outcome:` == "Died", "1", "0")

Step 2: Clean Data

three of four

#rbind() is for combing two data sets. The columns must match
ped_total <- rbind(ped_one2, ped_two2)

#convert admission date to a date format
ped_total$'Admission Date' <- as.Date(ped_total$"Admission Date", "%m/%d/%Y")

Step 2: Clean Data

four of four

colnames(ped_total) <- c("adm_date", 
                         "age",
                         "gender",
                         "surg_date",
                         "outcome",
                         "surg",
                         "death")

ped_total$vol <- 1
ped_total$age <- as.integer(ped_total$age)
ped_total$surg <- as.integer(ped_total$surg)
ped_total$death <- as.integer(ped_total$death)
ped_total$vol <- as.integer(ped_total$vol)

#Reduce dataset to include only toto patients
peds_total2 <- subset(ped_total, age < 18 )

Step 3: Data Wrangling

ped_trend <- peds_total2 %>%
  group_by(month=floor_date(adm_date, "month")) %>%
  summarise(death_sum = sum(death, na.rm = TRUE),
            volume_month = sum(vol, na.rm = TRUE),
            surg_sum = sum(surg, na.rm = TRUE),
            death_prop = sum(death, na.rm = TRUE) / sum(vol, na.rm = TRUE)
            )

Step 4: Prep data for a table

ped_trend2 <- ped_trend[c(1:10) , ]
ped_trend3 <- ped_trend2[ , c(1, 3, 4, 2, 5)]


colnames(ped_trend3) <- c("Time",
                          "Admissions",
                          "Received Surgery",
                          "Deaths",
                          "Mortality Proprotion")

Step 5: Make an awesome table

ped_trend3 %>%
  kable() %>%
      kable_styling(bootstrap_options = c("striped", "hover", "responsive"), full_width = F) %>%
    add_header_above(c( "Pediatric Neurotrauma at MRRH" = 5)) 
Pediatric Neurotrauma at MRRH
Time Admissions Received Surgery Deaths Mortality Proprotion
2018-01-01 7 1 0 0.0000000
2018-02-01 8 3 0 0.0000000
2018-03-01 9 2 0 0.0000000
2018-04-01 7 1 0 0.0000000
2018-05-01 11 0 1 0.0909091
2018-06-01 19 0 2 0.1052632
2018-07-01 16 1 1 0.0625000
2018-08-01 14 1 0 0.0000000
2018-09-01 13 2 0 0.0000000
2018-10-01 15 0 0 0.0000000

Knit File

Transfer to word

  • Open a word document