Last active
July 1, 2019 14:25
-
-
Save benmarwick/4a7d3556cf80a89981220912740b4971 to your computer and use it in GitHub Desktop.
Tidily summarizing multiple metric attributes of artefacts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Summarising multiple metric attributes of artefacts | |
p1 <- read_excel("data/riumailuoi.xlsx", sheet = 'phase1') | |
summary_stats_table <- function(x) { | |
x %>% | |
select(Mass, | |
Length, | |
Width, | |
Thickness, | |
Scars) %>% | |
filter_all(all_vars(!is.na(.))) %>% | |
summarise_all(c("min", | |
"mean", | |
"max", | |
"median", | |
"IQR")) %>% | |
gather(key = "key", | |
value = "value") %>% | |
separate(key, | |
into = c("measurement", | |
"statistic"), | |
sep = "[_]") %>% | |
spread(statistic, | |
value) | |
} | |
summary_stats_table(p1) | |
# A tibble: 5 x 6 | |
measurement IQR max mean median min | |
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> | |
1 Length 5.05 18.0 12.2 11.3 7.74 | |
2 Mass 450 1500 525 385 80 | |
3 Scars 7 37 15 12 7 | |
4 Thickness 1.03 4.04 2.76 2.52 1.21 | |
5 Width 2.34 9.9 7.04 6.88 4.33 | |
# or we can summarise more compactly: | |
summary_stats_table(p1) %>% | |
mutate(`median (IQR)` = str_glue('{median} ({round(IQR,2)})')) %>% | |
select(measurement, `median (IQR)`) | |
# A tibble: 5 x 2 | |
measurement `median (IQR)` | |
<chr> <S3: glue> | |
1 Length 11.315 (5.05) | |
2 Mass 385 (450) | |
3 Scars 12 (7) | |
4 Thickness 2.52 (1.03) | |
5 Width 6.875 (2.34) | |
# replace all NA by zero: mutate_all(coalesce, 0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment