-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSession1_Titanic_Follow_along.Rmd
189 lines (124 loc) · 3.31 KB
/
Session1_Titanic_Follow_along.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
---
title: "Session1_Titanic_Follow_along"
author: "Lauren Yee"
date: "13/07/2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
```
#Example Data
##Titanic Data Set
```{r read, echo=FALSE, warning=FALSE, message=FALSE}
titanic<-read_csv("./Raw Data/titanic.csv")
```
```{r DT, echo=FALSE,out.width="50%",fig.align='center'}
knitr::kable(head(titanic),'html')
```
## `filter` to select a subset of rows
## passengers who were 35 years old
```{r, echo=TRUE, out.width="75%"}
titanic %>%
filter(Age == 35) #<<
```
## `filter` for many conditions at once
## Age is 35 and Sex is female
```{r, echo=TRUE, out.width="75%"}
titanic %>%
filter(Age == 35, Sex=="female")#<<
```
]
operator | definition || operator | definition
------------|------------------------------||--------------|----------------
`<` | less than ||`x` | `y` | `x` OR `y`
`<=` | less than or equal to ||`is.na(x)` | test if `x` is `NA`
`>` | greater than ||`!is.na(x)` | test if `x` is not `NA`
`>=` | greater than or equal to ||`x %in% y` | test if `x` is in `y`
`==` | exactly equal to ||`!(x %in% y)` | test if `x` is not in `y`
`!=` | not equal to ||`!x` | not `x`
`x & y` | `x` AND `y` || |
## `select` to keep variables
```{r, echo=TRUE}
titanic %>%
filter(Age == 35, Sex=="female")%>%
select(Name, Sex, Age, Fare)#<<
```
## `select` to exclude variables
```{r, echo=TRUE}
titanic %>%
select(-Embarked)
```
## `select` a range of variables
```{r, echo=TRUE}
titanic %>%
select(PassengerId:Name)
```
## `slice` for certain row numbers
##First five
```{r, echo=TRUE}
titanic %>%
slice(1:5)
```
## `slice` for certain row numbers
##Last five
```{r, echo=TRUE}
last_row <- nrow(titanic)
titanic %>%
slice((last_row - 4):last_row)
```
## `pull` to extract a column as a vector
```{r, echo=TRUE}
titanic %>%
slice(1:6) %>%
pull(Fare)
```
##vs.
```{r, echo=TRUE}
titanic %>%
slice(1:6) %>%
select(Fare)
```
## `sample_n` / `sample_frac` for a random sample
## - `sample_n`: randomly sample 5 observations
```{r, echo=TRUE}
titanic_n5 <- titanic %>%
sample_n(5, replace = FALSE)
dim(titanic_n5)
```
## - `sample_frac`: randomly sample 20% of observations
```{r, echo=TRUE}
titanic_perc20 <-titanic %>%
sample_frac(0.2, replace = FALSE)
dim(titanic_perc20)
```
---
class: bg-main3 black
## `distinct` to filter for unique rows
##And `arrange` to order alphabetically
```{r, echo=TRUE}
titanic %>%
select(Pclass, Fare) %>%
distinct() %>%
arrange(Fare, Pclass)
```
## `summarise` to reduce variables to values
```{r, echo=TRUE}
titanic %>%
summarise(avg_fare = mean(Fare,na.rm=T))
```
## `group_by` to do calculations on groups
```{r, echo=TRUE}
titanic %>%
group_by(Sex) %>%
summarize(avg_fare = mean(Fare,na.rm=T))
```
## `count` observations in groups
```{r, echo=TRUE}
titanic %>%
count(Sex)
```
```{r, echo=TRUE}
titanic %>%
count(Survived)
```