V is for Verbs
This article is originally published at http://www.deeplytrivial.com/In this series, I've covered five terms for data manipulation:
These are the verbs that make up the grammar of data manipulation. They all work with group_by to perform these functions groupwise.
There are scoped versions of these verbs, which add _all, _if, or _at, that allow you to perform these verbs on multiple variables simultaneously. For instance, I could get means for all of my numeric variables like this. (Quick note: I created an updated reading dataset that has all publication years filled in. You can download it here.)
library(tidyverse)
reads2019 <- read_csv("~/Downloads/Blogging A to Z/SaraReads2019_allchanges.csv",
col_names = TRUE)
reads2019 %>%
summarise_if(is.numeric, list(mean))
## # A tibble: 1 x 13
## Pages Book.ID AverageRating OriginalPublica… read_time MyRating Gender Fiction
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 341. 1.36e7 3.94 1989. 3.92 4.14 0.310 0.931
## # … with 5 more variables: Childrens <dbl>, Fantasy <dbl>, SciFi <dbl>,
## # Mystery <dbl>, SelfHelp <dbl>
reads2019 %>%
summarise_at(vars(Pages, AverageRating, read_time, MyRating), list(mean))
## # A tibble: 1 x 4
## Pages AverageRating read_time MyRating
## <dbl> <dbl> <dbl> <dbl>
## 1 341. 3.94 3.92 4.14
numeric_summary <- reads2019 %>%
summarise_at(vars(Pages, AverageRating, read_time, MyRating), list("mean" = mean, "median" = median))
Next week is the last week of Blogging A to Z! See you then!
Thanks for visiting r-craft.org
This article is originally published at http://www.deeplytrivial.com/
Please visit source website for post related comments.