-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataCleaning_Candy.Rmd
358 lines (291 loc) · 11.8 KB
/
DataCleaning_Candy.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
---
title: "Data Cleaning: Candy"
author:
- name: Shannon E. Ellis
affiliation: Johns Hopkins Bloomberg School of Public Health
email: shannon0ellis@gmail.com
output:
BiocStyle::pdf_document:
code_folding: show
editor_options:
chunk_output_type: inline
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(janitor)
# library(skimr)
library(readr)
library(stringr)
library(reshape2)
library(readxl)
library(ggplot2)
library(dplyr)
## Set colors
## import colors to use
bright= c(red=rgb(222,45,38, maxColorValue=255), #de2d26
pink=rgb( 255, 102, 153, maxColorValue=255), #ff6699
orange=rgb(232,121,12, maxColorValue=255), #e8790c
yellow=rgb(255,222,13, maxColorValue=255), #ffde0d
green=rgb(12,189,24, maxColorValue=255), #0cbd18
teal=rgb(59,196,199, maxColorValue=255), #3bc4c7
blue=rgb(58,158,234, maxColorValue=255), #3a9eea
purple=rgb(148,12,232, maxColorValue=255)) #940ce8
```
# Introduction: So Much Candy
These data can be dowloaded by year [here](http://www.scq.ubc.ca/so-much-candy-data-seriously/); however, the code below also includes lines to download each dataset. For each year, the
“Net Feelies” metric is calculated as : (=#JOY-#DESPAIR)
# Data Wrangling
## 2017
```{r 2017, warning=FALSE, message=FALSE}
## download data from the Internet
if(!file.exists('candyhierarchy2017.csv')) {
system('wget http://www.scq.ubc.ca/wp-content/uploads/2017/10/candyhierarchy2017.csv')
}
## read data into R
candy17 <- read_csv("candyhierarchy2017.csv")
names(candy17) <- names(candy17) %>% str_replace_all("\\\\xd5", "")
candy17 <- as.data.frame(candy17)
## determine which columsn we're interested in
cols_to_summarize <- grep("Q6", colnames(candy17))
## let's clean up those columns
df <- candy17[,cols_to_summarize] %>% clean_names()
colnames(df) <- gsub("q6_","",colnames(df))
## function to summarize despair and joy
summary_function <- function(x){
a <- tabyl(x) %>%
melt(id="x") %>%
select(value) %>%
t()
return(a)
}
## get the names of all the columns we'll have in our output object
## who knows why I did it this way ¯\_(ツ)_/¯
x <- df[,2]
nameit <- tabyl(x) %>%
melt(id="x") %>%
mutate(category = paste0(x,"_",variable)) %>%
select(category)
## run function to summarize joy and despair
output17 <- as.data.frame(t(apply(df, 2, summary_function)))
colnames(output17) <- nameit$category
## calculate net feelies
feelies17 = as.data.frame(as.numeric(output17$JOY_n - output17$DESPAIR_n))
rownames(feelies17) <- rownames(output17)
```
## 2016
```{r 2016, warning=FALSE, message=FALSE}
## download data from the Internet
if(!file.exists('BOING-BOING-CANDY-HIERARCHY-2016-SURVEY-Responses.xlsx')) {
system('wget https://www.scq.ubc.ca/wp-content/uploads/2016/10/BOING-BOING-CANDY-HIERARCHY-2016-SURVEY-Responses.xlsx')
}
## read data into R
candy16 <- read_excel("BOING-BOING-CANDY-HIERARCHY-2016-SURVEY-Responses.xlsx") %>% clean_names()
candy16 <- as.data.frame(candy16)
candy16 <- candy16[, !apply(is.na(candy16), 2, all)]
## get the columns you're interested in
cols_to_summarize <- grep("x_", colnames(candy16))
df16 <- candy16[,cols_to_summarize]
colnames(df16)<-gsub("x_","",colnames(df16))
## use that function from above to summarize joy and despair for each item
output16 <- as.data.frame(t(apply(df16, 2, summary_function)))
colnames(output16) <- nameit$category
## calculate net_feeles
feelies16 = as.data.frame(as.numeric(output16$JOY_n - output16$DESPAIR_n))
rownames(feelies16) <- rownames(output16)
```
## 2015
```{r 2015, warning=FALSE, message=FALSE}
## download data from the Internet
if(!file.exists('CANDY-HIERARCHY-2015-SURVEY-Responses.xlsx')) {
system('wget https://www.scq.ubc.ca/wp-content/uploads/2015/10/CANDY-HIERARCHY-2015-SURVEY-Responses.xlsx')
}
## read data into R
candy15 <- read_excel("CANDY-HIERARCHY-2015-SURVEY-Responses.xlsx") %>% clean_names()
## get the columns you're interested in
cols_to_summarize <- grep("x_", colnames(candy15))
df15 <- as.data.frame(candy15[,cols_to_summarize])
colnames(df15)<-gsub("x_","",colnames(df15))
## see note in section above
x <- df15[,2]
nameit <- tabyl(x) %>%
melt(id="x") %>%
mutate(category = paste0(x,"_",variable)) %>%
select(category)
## use that function from above to summarize joy and despair for each item
output15 <- as.data.frame(t(apply(df15, 2, summary_function)))
colnames(output15) <- nameit$category
## calculate net feelies
feelies15 = as.data.frame(as.numeric(output15$JOY_n - output15$DESPAIR_n))
rownames(feelies15) <- rownames(output15)
```
## 2014
```{r 2014, warning=FALSE, message=FALSE}
## download data from the Internet
if(!file.exists('CANDYDATA.xlsx')) {
system('wget http://www.scq.ubc.ca/wp-content/uploads/2014/10/CANDYDATA.xlsx')
}
## read data into R & clean it up a little
candy14 <- read_excel("CANDYDATA.xlsx") %>% clean_names %>%
as.data.frame %>%
filter(!is.na(item))
rownames(candy14) <- candy14$item
candy14 <- as.data.frame(t(candy14))
candy14<-clean_names(candy14)
colnames(candy14)<-gsub("x100","100",colnames(candy14))
candy14 <- as.data.frame(t(candy14))
## net feelies have already been calculated
feelies14 <- as.data.frame(as.numeric(as.character(candy14$net_feelies)))
rownames(feelies14) <- rownames(candy14)
```
# Every Year Data
Here, we're only looking at items included on each year's survey. Candy's that were not on every survey from 2014-2017 have been excluded.
## Merging & Scaling Data
To complete this analysis, all four years of "feelies" data are combined. They are then scaled within each year so that the most favorable candy (brings the most relative "JOY") receives a score of +1 while the least favorable (brings the most relative "DESPAIR") receives a score of -1.
```{r data-merge}
## merge all these data together
combined <- merge(feelies14,feelies15,by="row.names") %>%
merge(.,feelies16,by.x="Row.names",by.y="row.names") %>%
merge(.,feelies17,by.x="Row.names",by.y="row.names")
combined <- as.data.frame(combined)
colnames(combined) <- c("row.names","x2014","x2015","x2016","x2017")
## scale all values between -1 and 1 within a year
scalefunc <- function(x){
minx = min(x)
maxx = max(x)
val= 2*((x - minx)/(maxx - minx))-1
return(val)
}
out <- as.data.frame(apply(combined[,2:5],2,scalefunc))
out$row.names <- as.character(combined$row.names)
## calculate a few things we may use to order the graph
out$biggest_change <- out$x2016-out$x2015
out$abs_change <- abs(out$x2016-out$x2015)
out$sum <- rowSums(out[,1:4])
out <- out %>% arrange(sum)
```
## Data Visualization
Having calculated the scaled feelies metric, the data are then visualized.
```{r data-viz}
## tidy data format!
data <- melt(out, id.vars='row.names', variable.name='Year',
value.name="net_feelies") %>%
mutate(item_name = gsub("_", " ",row.names))
## let's plot!
theme_set(theme_classic())
p <- ggplot(out, aes(x=factor(item_name,level=row.names))) +
geom_point(aes(y=x2014), col="tomato2", size=3) +
geom_point(aes(y=x2015), col="orange", size=3) +
geom_point(aes(y=x2016), col="springgreen", size=3) +
geom_point(aes(y=x2017), col="dodgerblue", size=3) +
scale_colour_manual("",
breaks = c("2014", "2014", "2016", "2017"),
values = c("tomato2", "orange", "springgreen","dodgerblue")) +
geom_segment(aes(x=item_name,
xend=item_name,
y=min(x2014),
yend=max(x2014)),
linetype="dashed",
size=0.1) + # Draw dashed lines
labs(title="Candy Preferences Over Time",
subtitle="So Much Candy Survey",
caption="source: http://www.scq.ubc.ca/so-much-candy-data-seriously/",
x="",y="") +
coord_flip() +
guides(colour = guide_legend())
p1 <- data %>%
filter(Year=="x2014" | Year=="x2015" | Year=="x2016" | Year=="x2017") %>%
ggplot(., aes(x=factor(item_name,level=unique(item_name)),
y = net_feelies, color = Year)) +
geom_point(size=3) +
scale_colour_manual(
labels = c("x2014"="2014", "x2015"="2015",
"x2016"="2016", "x2017"="2017"),
values = c("x2014"="tomato2", "x2015"="orange",
"x2016"="forestgreen","x2017"="dodgerblue")) +
geom_segment(aes(x=item_name,
xend=item_name,
y=min(net_feelies),
yend=max(net_feelies)),
linetype="dashed",
size=0.1) + # Draw dashed lines
labs(title="Candy Preferences Over Time",
subtitle="So Much Candy Survey",
caption="source: http://www.scq.ubc.ca/so-much-candy-data-seriously/",
x="",y="") +
coord_flip() +
theme(legend.position = c(-1.0, 0.9),
legend.text=element_text(size=16),
axis.text.x=element_text(size=16),
axis.text.y=element_text(size=11),
legend.title=element_text(size=20),
plot.title=element_text(size=24,hjust = 4),
plot.subtitle = element_text(size=16,hjust = -1.4))
ggsave(plot=p1, filename="images/candy_alldata.png", height=13,width=9,units=c("in"))
```
![](images/candy_alldata.png)
# All Data
Below, instead of limiting to those items that were incluced on the survey all four years, here we summarize all items, using the same process as above
## Merging & Scaling Data
The data are first merged and scaled within year.
```{r data-merge-all}
## merge all these data together
combined_all <- merge(feelies14,feelies15,by="row.names", all=TRUE) %>%
merge(.,feelies16,by.x="Row.names",by.y="row.names", all=TRUE) %>%
merge(.,feelies17,by.x="Row.names",by.y="row.names", all=TRUE)
combined_all <- as.data.frame(combined_all)
colnames(combined_all) <- c("row.names","x2014","x2015","x2016","x2017")
## scale all values between -1 and 1 within a year
scalefunc <- function(x){
minx = min(x, na.rm=TRUE)
maxx = max(x, na.rm=TRUE)
val= 2*((x - minx)/(maxx - minx))-1
return(val)
}
out_all <- as.data.frame(apply(combined_all[,2:5],2,scalefunc))
out_all$row.names <- as.character(combined_all$row.names)
## calculate a few things we may use to order the graph
## calculate sum, normalized by number of years question was asked
out_all$sum <- rowSums(out_all[,1:4],na.rm=TRUE)
isNA <- function(x){sum(is.na(x))}
out_all$isNA <- apply(out_all,1,isNA)
out_all$sum_NA <- out_all$sum/(4-out_all$isNA)
out_all <- out_all %>% arrange(sum_NA)
```
## Data Visualization
The data are then visualized.
```{r data-viz-all}
## tidy data format!
data_all <- melt(out_all, id.vars='row.names',
variable.name='Year', value.name="net_feelies") %>%
mutate(item_name = gsub("_", " ",row.names))
## let's plot!
p2 <- data_all %>%
filter(Year=="x2014" | Year=="x2015" | Year=="x2016" | Year=="x2017") %>%
ggplot(., aes(x=factor(item_name,level=unique(item_name)), y = net_feelies, color = Year)) +
geom_point(size=3) +
scale_colour_manual(
labels = c("x2014"="2014", "x2015"="2015",
"x2016"="2016", "x2017"="2017"),
values = c("x2014"="tomato2", "x2015"="orange",
"x2016"="forestgreen","x2017"="dodgerblue")) +
geom_segment(aes(x=item_name,
xend=item_name,
y=-1,
yend=1),
linetype="dashed",
size=0.1) + # Draw dashed lines
labs(title="Candy Preferences Over Time",
subtitle="So Much Candy Survey",
caption="source: http://www.scq.ubc.ca/so-much-candy-data-seriously/",
x="",y="") +
coord_flip() +
theme(legend.position = c(-0.6, 0.9),
legend.text=element_text(size=16),
axis.text.x=element_text(size=16),
axis.text.y=element_text(size=9),
legend.title=element_text(size=20),
plot.title=element_text(size=24,hjust = -2.3),
plot.subtitle = element_text(size=16,hjust = -0.4))
ggsave(plot=p2, filename="images/candy_alldata_allcandy.png", height=17,width=11,units=c("in"))
```
![](images/candy_alldata_allcandy.png)