Below are a couple of functions I use for summarizing variables. These are useful for evaluating characteristics in different strata.
iqr4tab()
This function calculates the median and 25th, 75th quartile (IQR) and formats the output for a table:
iqr4tab <- function(x, digits = 2, na.rm = TRUE) {
nums <- fivenum(x, na.rm = na.rm)[2:4]
nums <- round(nums, digits)
paste0(nums[2], " (", nums[1], ",", nums[3], ")")
}
prop4tab()
This function will calculate count and proportion for both levels of a binary variable and format for a table:
prop4tab <- function(x, level = "1", digits = 1) {
x <- as.factor(x)
n_total <- length(x)
if (is.na(level) | level == "NA") {
level <- "NA"
n_level1 <- length(x[x==level])
} else {
n_level1 <- length(x[x==level & !is.na(x)])
}
prop <- n_level1/n_total
paste0(n_level1, " (", round(prop*100,digits),")")
}
Example
The following example uses data from the AmesHousing
package:
library(AmesHousing)
library(dplyr)
library(tidyr)
ames <- make_ames()
ames %>%
mutate(House_Style = ifelse(House_Style == "One_Story","single story", "multi story")) %>%
group_by(House_Style) %>%
summarise(Price = iqr4tab(Sale_Price))
## # A tibble: 2 x 2
## House_Style Price
## <chr> <chr>
## 1 multi story 167500 (132000,213000)
## 2 single story 155000 (127000,213750)
ames %>%
mutate(House_Style = ifelse(House_Style == "One_Story","single story", "multi story")) %>%
group_by(House_Style) %>%
summarise(Central_Air = prop4tab(Central_Air, level = "Y"))
## # A tibble: 2 x 2
## House_Style Central_Air
## <chr> <chr>
## 1 multi story 1344 (92.8)
## 2 single story 1390 (93.9)
ames %>%
mutate(House_Style = ifelse(House_Style == "One_Story", "single story", "multi story"),
Fireplace = ifelse(Fireplaces == 0, 0, 1)) %>%
group_by(House_Style) %>%
summarise(Price = iqr4tab(Sale_Price),
Rooms = iqr4tab(TotRms_AbvGrd),
AC = prop4tab(Central_Air, level = "Y"),
Fireplace = prop4tab(Fireplace)) %>%
gather(variable, summary, -House_Style) %>%
spread(House_Style, summary)
## # A tibble: 4 x 3
## variable `multi story` `single story`
## <chr> <chr> <chr>
## 1 AC 1344 (92.8) 1390 (93.9)
## 2 Fireplace 801 (55.3) 707 (47.7)
## 3 Price 167500 (132000,213000) 155000 (127000,213750)
## 4 Rooms 7 (6,8) 6 (5,7)