G is for group_by
This article is originally published at http://www.deeplytrivial.com/For the letter G, I'd like to introduce a very useful function: group_by. This function lets you group data by one or more variables. By itself, it may not seem very useful, but it's great when you start manipulating and summarizing data. That's because many of the functions applied to data after you used group_by are done groupwise. First, let's demonstrate the effect group_by has on a filter. I'll load my reading dataset and group it by the Fiction flag (so 1 means the book was fiction and 0 means it was non-fiction). What was the longest book I read in each of those categories?
library(tidyverse)
reads2019 <- read_csv("~/Downloads/Blogging A to Z/SaraReads2019_allrated.csv", col_names = TRUE)
reads2019 %>%
group_by(Fiction) %>%
filter(Pages == max(Pages)) %>%
select(Title, Pages, MyRating, Fiction)
## # A tibble: 2 x 4
## # Groups: Fiction [2]
## Title Pages MyRating Fiction
## <chr> <dbl> <dbl> <dbl>
## 1 How Music Works 345 5 0
## 2 It 1156 4 1
I can also group by multiple variables, and use that to summarize my data. Let's see what happens when I use two of my genre variables for grouping, then generate a count of the number of books in each group.
reads2019 %>%
group_by(Childrens, Fantasy) %>%
summarise(count = n())
## # A tibble: 4 x 3
## # Groups: Childrens [2]
## Childrens Fantasy count
## <dbl> <dbl> <int>
## 1 0 0 49
## 2 0 1 21
## 3 1 0 1
## 4 1 1 16
reads2019 %>%
arrange(desc(Pages)) %>%
group_by(Childrens, Fantasy) %>%
summarise(count = n(),
title = first(Title),
Pages = first(Pages))
## # A tibble: 4 x 5
## # Groups: Childrens [2]
## Childrens Fantasy count title Pages
## <dbl> <dbl> <int> <chr> <dbl>
## 1 0 0 49 The Robber Bride 528
## 2 0 1 21 It 1156
## 3 1 0 1 The Queen's Corgi: On Purpose 181
## 4 1 1 16 The Patchwork Girl of Oz (Oz, #7) 346
See you tomorrow when we talk about reading in different file types with the haven package!
Thanks for visiting r-craft.org
This article is originally published at http://www.deeplytrivial.com/
Please visit source website for post related comments.