AMR/vignettes/freq.R

90 lines
2.8 KiB
R
Raw Normal View History

2018-05-09 11:44:46 +02:00
## ----setup, include = FALSE, results = 'markup'--------------------------
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#"
)
library(dplyr)
library(AMR)
## ---- echo = TRUE, results = 'hide'--------------------------------------
# just using base R
2018-05-09 11:44:46 +02:00
freq(septic_patients$sex)
2018-07-01 21:40:37 +02:00
# using base R to select the variable and pass it on with a pipe from the dplyr package
2018-05-09 11:44:46 +02:00
septic_patients$sex %>% freq()
2018-07-01 21:40:37 +02:00
# do it all with pipes, using the `select` function from the dplyr package
2018-05-09 11:44:46 +02:00
septic_patients %>%
select(sex) %>%
freq()
2018-07-01 21:40:37 +02:00
# or the preferred way: using a pipe to pass the variable on to the freq function
septic_patients %>% freq(sex) # this also shows 'age' in the title
2018-05-09 11:44:46 +02:00
## ---- echo = TRUE--------------------------------------------------------
freq(septic_patients$sex)
## ---- echo = TRUE, results = 'hide'--------------------------------------
2018-07-01 21:40:37 +02:00
my_patients <- septic_patients %>% left_join_microorganisms()
2018-05-09 11:44:46 +02:00
## ---- echo = TRUE--------------------------------------------------------
colnames(microorganisms)
## ---- echo = TRUE--------------------------------------------------------
dim(septic_patients)
dim(my_patients)
## ---- echo = TRUE--------------------------------------------------------
2018-07-01 21:40:37 +02:00
my_patients %>% freq(genus, species)
2018-05-09 11:44:46 +02:00
## ---- echo = TRUE--------------------------------------------------------
# # get age distribution of unique patients
septic_patients %>%
distinct(patient_id, .keep_all = TRUE) %>%
2018-07-01 21:40:37 +02:00
freq(age, nmax = 5)
2018-05-09 11:44:46 +02:00
## ---- echo = TRUE--------------------------------------------------------
septic_patients %>%
2018-07-01 21:40:37 +02:00
freq(hospital_id)
2018-05-09 11:44:46 +02:00
## ---- echo = TRUE--------------------------------------------------------
septic_patients %>%
2018-07-01 21:40:37 +02:00
freq(hospital_id, sort.count = TRUE)
2018-05-09 11:44:46 +02:00
## ---- echo = TRUE--------------------------------------------------------
septic_patients %>%
select(amox) %>%
freq()
## ---- echo = TRUE--------------------------------------------------------
septic_patients %>%
select(date) %>%
freq(nmax = 5)
## ---- echo = TRUE--------------------------------------------------------
2018-07-01 21:40:37 +02:00
my_df <- septic_patients %>% freq(age)
class(my_df)
2018-05-09 11:44:46 +02:00
## ---- echo = TRUE--------------------------------------------------------
2018-07-01 21:40:37 +02:00
dim(my_df)
2018-05-09 11:44:46 +02:00
## ---- echo = TRUE--------------------------------------------------------
septic_patients %>%
2018-07-01 21:40:37 +02:00
freq(amox, na.rm = FALSE)
2018-05-09 11:44:46 +02:00
## ---- echo = TRUE--------------------------------------------------------
2018-07-01 21:40:37 +02:00
septic_patients %>%
freq(hospital_id, row.names = FALSE)
2018-05-09 11:44:46 +02:00
2018-07-01 21:40:37 +02:00
## ---- echo = TRUE--------------------------------------------------------
septic_patients %>%
freq(hospital_id, markdown = TRUE)
2018-05-09 11:44:46 +02:00
## ---- echo = FALSE-------------------------------------------------------
# this will print "2018" in 2018, and "2018-yyyy" after 2018.
yrs <- c(2018:format(Sys.Date(), "%Y"))
yrs <- c(min(yrs), max(yrs))
yrs <- paste(unique(yrs), collapse = "-")