Skip to content

Commit

Permalink
final spread/gather -> pivots
Browse files Browse the repository at this point in the history
  • Loading branch information
riinuots committed Aug 14, 2020
1 parent 4d83024 commit f534408
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
7 changes: 4 additions & 3 deletions 06_working_continuous.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -594,10 +594,11 @@ africa2002 <- gapdata %>% # save as africa2002
head(africa2002) # inspect
africa2002 %>%
gather(key, lifeExp, -country) %>% # gather values to same column
ggplot(aes(x = lifeExp)) +
# pivot lifeExp and lifeExp_log values to same column (for easy plotting):
pivot_longer(contains("lifeExp")) %>%
ggplot(aes(x = value)) +
geom_histogram(bins = 15) + # make histogram
facet_wrap(~key, scales = "free") # facet with axes free to vary
facet_wrap(~name, scales = "free") # facet with axes free to vary
```

Expand Down
6 changes: 3 additions & 3 deletions 09_logistic_regression.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ melanoma %>%
mort_5yr.num = as.numeric(mort_5yr) - 1
) %>%
select(mort_5yr.num, age, year) %>%
gather(key = "predictors", value = "value", -mort_5yr.num) %>%
pivot_longer(all_of(c("age", "year")), names_to = "predictors") %>%
ggplot(aes(x = value, y = mort_5yr.num)) +
geom_point(size = 0.5, alpha = 0.5) +
geom_smooth(method = "loess") +
Expand Down Expand Up @@ -509,11 +509,11 @@ select_explanatory <- c("ulcer.factor", "sex.factor", "t_stage.factor")
melanoma %>%
select(one_of(select_explanatory)) %>%
gather(key, value, -sex.factor) %>%
pivot_longer(-sex.factor) %>%
ggplot(aes(value, fill = sex.factor)) +
geom_bar(position = "fill") +
ylab("proportion") +
facet_wrap(~key, scale = "free", ncol = 2) +
facet_wrap(~name, scale = "free", ncol = 2) +
coord_flip()
```

Expand Down
2 changes: 1 addition & 1 deletion data/gbd_process.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ gbd_short %>%
gbd_long %>%
write_csv(path = "data/global_burden_disease_cause-year-sex-income.csv")

#Chapter 3 - spread() and gather()
#Chapter 3 - pivot_wider() and pivot_longer()
gbd_long_example = gbd_long %>%
filter(year %in% c(1990, 2017)) %>%
group_by(cause, sex, year) %>%
Expand Down

0 comments on commit f534408

Please sign in to comment.