A few times in this series, I’ve wanted to display part of a dataset, such as key variables, like Title, Rating, and Pages. The tidyverse allows you to easily keep or drop variables, either temporarily or permanently, with the select function. For instance, we can use select along with other tidyverse functions to create a quick descriptive table of my dataset. Let’s filter down to books that are fantasy and/or sci-fi and that took me the longest to read, then select a few descriptives to display.
reads2019 <- read_csv("~/Downloads/Blogging A to Z/SaraReads2019_allrated.csv", col_names = TRUE)
group_by(Fantasy, SciFi) %>%
filter(read_time == max(read_time) & (Fantasy == 1 | SciFi == 1)) %>%
select(Title, Author, Pages, read_time)
## # A tibble: 4 x 6
## # Groups: Fantasy, SciFi 
## Fantasy SciFi Title Author Pages read_time
## 1 1 1 1Q84 Murakami, Ha… 925 7
## 2 0 1 The End of All Things (Old Man's … Scalzi, John 380 10
## 3 0 1 The Long Utopia (The Long Earth #… Pratchett, T… 373 10
## 4 1 0 Tik-Tok of Oz (Oz, #8) Baum, L. Fra… 272 25
Of course, I can also permanently change the reads2019 dataset to only keep those variables or create a new dataset with just those variables. The select function can also be used to drop single variables, by putting a – sign before the variable name. Let’s say I decided I no longer wanted to keep the Self Help genre flag. I could throw that out of my dataset like this.
reads2019 <- reads2019 %>%
That variable is now gone. You can use this same code to drop multiple variables at once, by putting – signs before each variable name.
small_reads2019 <- reads2019 %>%
select(-AdditionalAuthors, -AverageRating, -OriginalPublicationYear)
Whichever you do, keeping or dropping, choose the option that minimizes how many things you have to type. If you have a large number of variables and want a dataset with only a handful, I’d use the names of the variables I want to keep with select. If you only want to drop 1 or 2 variables, using select to drop will be faster.
Tomorrow we’ll talk about a variable transformation that makes plotting skewed variables much easier. Stay tuned!