-
Notifications
You must be signed in to change notification settings - Fork 13
/
class_3.Rmd
297 lines (261 loc) · 7.14 KB
/
class_3.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
---
title: "in class coding"
author: "Julia Gallucci"
date: "13/12/2023"
output: html_document
---
Load our libraries
```{r}
#install.packages("haven")
library(haven)
library(tidyverse)
```
Read in our datasets
```{r}
ads_data <- read_sav("./ads_raw.sav")
ces_2019_raw <- read_csv("./ces_2019_raw.csv")
```
Let's have a look!
```{r}
glimpse(ads_data)
```
Filter our data
```{r}
filter(ads_data, Duration__in_seconds_ < 100)
```
Arrange our data
```{r}
arrange(ads_data, Duration__in_seconds_) #ascending
arrange(ads_data, -Duration__in_seconds_) #descending
```
Select specific columns from our data
```{r}
select(ads_data, RecordedDate) #select columns
select(ads_data, -Consent, -DistributionChannel) #remove columns
```
Multiple operations using the pipe operator
```{r}
ads_data %>%
filter(Duration__in_seconds_ < 100) %>%
arrange(Duration__in_seconds_) %>%
select(RecordedDate, Duration__in_seconds_)
```
Mutate columns in our data
```{r}
ads_data <- ads_data %>%
mutate(Birthyear_add_day =
str_c(Birthyear, "07-01")) %>%
mutate(Birthyear_add_day =
as_datetime(Birthyear_add_day))
ads_data$Birthyear_add_day
```
```{r}
ads_data <- ads_data %>%
mutate(age = EndDate - Birthyear_add_day)
ads_data %>%
select(age)
```
Summarize our data
```{r}
summary(ads_data)
```
Pulling a variable from our dataset
```{r}
#MEDIAN
ads_data %>%
pull(Duration__in_seconds_) %>%
median(na.rm = TRUE)
#MEAN
ads_data %>%
pull(Duration__in_seconds_) %>%
mean(na.rm = TRUE)
#RANGE
ads_data %>%
pull(Duration__in_seconds_) %>%
range(na.rm = TRUE)
#VARIANCE
ads_data %>%
pull(Duration__in_seconds_) %>%
var(na.rm = TRUE)
#STANDARD DEVIATION
ads_data %>%
pull(Duration__in_seconds_) %>%
sd(na.rm = TRUE)
```
Summarise our data
```{r}
ads_data %>%
summarise(mean_time = mean(Duration__in_seconds_, na.rm = TRUE),
sd_time = sd(Duration__in_seconds_, na.rm = TRUE))
```
Grouping based on categorical variable
```{r}
ads_data %>%
group_by(Gender) %>%
summarise(counts = n(),
mean_time = mean(Duration__in_seconds_, na.rm = TRUE),
sd_time = sd(Duration__in_seconds_, na.rm = TRUE))
```
Data cleaning
```{r}
CES_data <- ces_2019_raw %>%
mutate(cps19_yob_fix = cps19_yob +1919)
CES_data %>%
pull(cps19_yob_fix) %>%
range(na.rm = TRUE)
```
```{r}
CES_data <- CES_data %>%
mutate(age = 2019 - cps19_yob_fix)
CES_data %>%
pull(age) %>%
range(na.rm = TRUE)
CES_data <- CES_data %>%
mutate(cps19_gender_fix = factor(cps19_gender)) %>%
mutate(cps19_gender_fix =
fct_recode(cps19_gender_fix,
"M" = "1",
"F"= "2",
"NB" = "3"
))
CES_data %>%
select(cps19_gender, cps19_gender_fix)
```
```{r}
CES_data %>%
filter(cps19_household > 10) %>%
arrange(-cps19_household) %>%
pull(cps19_household)
```
```{r}
CES_data <- CES_data %>%
mutate(cps19_household = ifelse(cps19_household > 15, NA, cps19_household))
CES_data %>%
filter(cps19_household > 10) %>%
pull(cps19_household)
```
```{r}
CES_data %>%
filter(cps19_income_number > 1000000) %>%
arrange(-cps19_income_number) %>%
pull(cps19_income_number)
```
```{r}
CES_data <- CES_data %>%
mutate(cps19_income_number = ifelse(cps19_income_number >= 100000000, NA, cps19_income_number))
CES_data %>%
filter(cps19_income_number > 1000000) %>%
pull(cps19_income_number)
```
Data summarizing
```{r}
CES_data <- ces_2019_cleaned
CES_data %>%
filter(cps19_province == "Ontario") %>%
select(cps19_prov_gov_sat,
cps19_prov_id,
cps19_income_number)
```
```{r}
CES_data %>%
filter(cps19_province == "Ontario") %>%
select(cps19_prov_gov_sat,
cps19_prov_id,
cps19_income_number) %>%
group_by(cps19_prov_id) %>%
summarise(median_income =
median(cps19_income_number, na.rm = TRUE), count = n())
```
```{r}
CES_data %>%
filter(cps19_province =="Ontario") %>%
select(cps19_prov_gov_sat,
cps19_prov_id,
cps19_income_number) %>%
mutate(cps19_prov_id = factor(cps19_prov_id, levels = c("Liberal","Progressive Conservative","NDP","Green","Another party","None", "Don't know/prefer not to answer"))) %>%
group_by(cps19_prov_id) %>%
summarise(median_income = median(cps19_income_number, na.rm = TRUE), count = n())
```
```{r}
CES_data %>%
filter(cps19_province == "Ontario") %>%
select(cps19_prov_gov_sat,
cps19_prov_id,
cps19_income_number) %>%
group_by(cps19_prov_gov_sat) %>%
summarise(median_income = median(cps19_income_number, na.rm = TRUE), count = n())
```
```{r}
CES_data %>%
filter(cps19_province == "Ontario") %>%
select(cps19_prov_gov_sat,
cps19_prov_id,
cps19_income_number) %>%
group_by(cps19_prov_id) %>%
summarise(median_income = median(cps19_income_number, na.rm = TRUE), count = n()) %>%
arrange(-count)
```
```{r}
CES_data %>%
filter(cps19_province == "Ontario") %>%
select(cps19_prov_gov_sat,
cps19_prov_id,
cps19_income_number) %>%
mutate(cps19_prov_id = factor(cps19_prov_id, levels = c("Liberal","Progressive Conservative","NDP","Green","Another party","None","Don't know/prefer not to answer"))) %>%
mutate(cps19_prov_gov_sat = factor(cps19_prov_gov_sat, levels = c("Not at all satisfied","Not very satisfied", "Fairly satisfied","Very satisfied","Don't know/prefer not to answer"))) %>%
group_by(cps19_prov_gov_sat, cps19_prov_id) %>%
summarise(median_income = median(cps19_income_number, na.rm = TRUE)) %>%
spread(key = cps19_prov_gov_sat,
value = median_income)
```
Exercises
1. Filter the rows in the CES_data where survey taker is between 30 and 50 years old
```{r}
CES_data %>%
filter(cps19_age > 30 & cps19_age < 50) %>%
select(cps19_age) %>%
pull(cps19_age) %>%
range()
```
2. Filter the rows in the CES_data where the survey taker answered the cps19_votechoice question (not NA)
```{r}
CES_data %>%
filter(!(is.na(cps19_votechoice))) %>%
select(cps19_votechoice)
```
3. Selecting the columns cps19_age and cps19_province from CES_data
```{r}
CES_data %>%
select(cps19_age, cps19_province)
```
4. Select all but cps19
```{r}
CES_data %>%
select(-cps19_province)
```
1. Create new variable in CES_data that tells us whether or not a person consumes news content (cps19_news_cons == 0 minutes OR not)
```{r}
CES_data %>%
mutate(news_watcher = ifelse(cps19_news_cons == "0 minutes", "Not watcher", "Watcher")) %>%
select(cps19_news_cons, news_watcher)
```
2. Modify the variable cps19_income_number so that its measured in 1000s
```{r}
CES_data %>%
mutate(cps19_income_number = cps19_income_number / 1000)
```
1. CES_data data; group by cps19_votechoice. find median and mean rating for Trudeau
```{r}
CES_data %>%
select(cps19_lead_rating_23, cps19_votechoice) %>%
group_by(cps19_votechoice) %>%
summarise(median_rating = median(cps19_lead_rating_23, na.rm = TRUE), mean_rating = mean(cps19_lead_rating_23, na.rm = TRUE))
```
2. Use the CES_data dataset. Group by cps19_imm and cps19_spend_educ. Find the count for each group.
```{r}
CES_data %>%
select(cps19_imm, cps19_spend_educ) %>%
group_by(cps19_imm, cps19_spend_educ) %>%
summarise(counts = n()) %>%
spread(key = cps19_imm, value = counts)
```