-
Notifications
You must be signed in to change notification settings - Fork 31
/
Zach-TidyVerseEXTENDED.Rmd
175 lines (137 loc) · 4.42 KB
/
Zach-TidyVerseEXTENDED.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
---
title: "TidyVerse"
author: "Zachary Safir, extended by Sam Reeves"
date: "4/23/2021"
output:
html_document:
df_print: paged
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE,message = F,warning = F)
```
```{r}
library(tidyverse)
```
## Introduction
| The tidyverse contains a collection of data science packages that work together in harmony to accomplish various goals. This vignette will demonstrate several ways to make full use of their combined capability.
## The Data
| For this demonstration, we will use a dataset that is included with dpylr itself. It contains data on the characters from the Starwars series. Specifically, various pieces of information that describe each character.
|
|
```{r}
starwars
```
|
|
| Interestingly, some of the columns are full of lists. The column displayed below, shows which films a character appeared in.
|
|
```{r}
head(starwars$films)
```
|
|
| The first thing to figure out is how to pick out only characters that appear in certian films. In order to use filter from dpylr on a list, we need to use a purr function with it. As filter is expecting a logical value, we need to return something logical. Using map_lgl, we can accomplish this.
|
|
```{r}
starwars %>%
filter(map_lgl(films,~ "Attack of the Clones" %in% .))
```
|
|
| In order to use filter on multiple values, we need to use the base R function "all".
|
|
```{r}
starwars %>%
filter(map_lgl(films,~ all( c("Attack of the Clones","A New Hope") %in% .)))
```
|
|
| We can also use tidyr in order to flatten our lists full of data out. The resulting dataframe of this action is shown below.
|
|
```{r}
starwars %>%
select(name,films) %>%
unnest(films)
```
|
|
| With our data in a normal format, we can use the dpylr count function to discover which film is most common.
|
|
```{r}
starwars %>%
unnest(films) %>%
count(films) %>%
arrange(n)
```
|
|
| Another interesting function comes from forcats. In the previous example, we had a small number of a categories. However, quite often we will have a handful of common categories, and a whole bunch of other smaller groups. In such a case, we can use the forcats fct_lump to grab the most common categories, and lump the least most into a Other category.
|
|
```{r}
starwars %>%
filter(!is.na(homeworld)) %>%
mutate(homeworld = fct_lump(homeworld, n = 3)) %>%
count(homeworld) %>%
arrange(n)
```
|
|
| Finally, we will demonstrate the fct_infreq function. In the first plot shown below, by default the plot is not ordered in any kind of way. However, by using fct_infreq in the second plot, we are able to reorder the values by their frequency in the data.
|
|
```{r}
starwars %>%
unnest(films) %>%
ggplot(aes(films)) +
geom_bar() +
coord_flip()
```
```{r}
starwars %>%
unnest(films) %>%
mutate(films = fct_infreq(films)) %>%
ggplot(aes(films)) +
geom_bar() +
coord_flip()
```
----------------------------
# Extension by Sam
Let's look at some of the attributes of the characters. I'm interested in know which of these folks could float. We can't know density directly, but we can fudge it a bit by comparing height and mass. I would like to plot this relationship and categorize the results by species.
Let's begin by removing outliers. There is one very heavy character, and no real obvious outliers in height.
```{r}
hist(starwars$mass)
hist(starwars$height)
sort(starwars$mass)
filter(starwars, mass == 1358)
```
Of course, our fat outlier is none other than Jabba the Hut. Where's the Rancor??? I suppose it doesn't matter.
```{r}
sw_no_jabba <- starwars %>%
filter(name != "Jabba Desilijic Tiure")
hist(sw_no_jabba$mass)
```
This is somewhat normally distributed.
There are so many species in starwars, we should show only the most common, and lump the rest into a group called "Other". If there is only one specimen, we call them "Other".
```{r}
top_species <- sw_no_jabba %>%
group_by(species) %>%
filter(n() >= 2)
tops <- unique(top_species$species)
sw_no_jabba <- sw_no_jabba %>%
mutate(species = fct_other(species, keep = tops, other_level = "Other"))
```
```{r}
ggplot(sw_no_jabba,
aes(x = height,
y = mass,
col = species)) +
geom_point() +
geom_smooth(method = "lm")
```
Assuming that humans offer a pretty good approximation of things that float, then we can probably assume that these droids do not float, but that most Gungans, Mirialans, Wookies, and Kaminoans do.