-
Notifications
You must be signed in to change notification settings - Fork 3
/
1-Importing & Merging Datasets.R
421 lines (344 loc) · 10.4 KB
/
1-Importing & Merging Datasets.R
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# ===== Introduction =====
# Project: Learning R by Doing - HarvardX-MITx Analysis
# By: Matthew Hendrickson
# What: Importing & Merging HxMITx Data Sources
# -----Set up environment -----
library(tidyverse)
# ===== Importing & Reviewing the Data =====
# ----- a. Import HXMITX course data into RStudio -----
HXMITX <- read_csv("HMXPC13_DI_v2_5-14-14.csv")
# View & get summary of HXMITX course data
View(HXMITX) # all rows
head(HXMITX) # first few rows
tail(HXMITX) # last few rows
summary(HXMITX) # basic summary info
# Drop roles as all records are NULL
HXMITX$roles <- NULL
# ----- b. Import reference data into RStudio -----
courses <- read_csv("hxmitx_course_reference.csv")
# View & get summary of reference data
View(courses) # all rows
head(courses) # first few rows
tail(courses) # last few rows
summary(courses) # basic summary info
# Drop institution to avoid duplication after join
courses$institution <- NULL
# ===== Add new fields =====
# For more on regex construction: https://regex101.com/
# ----- a. Exploration -----
# Gets every instance of HarvardX or MITx and prints it
str_extract_all(HXMITX$course_id, 'HarvardX')
str_extract_all(HXMITX$course_id, 'MITx')
# Sets True/False for each case where it matches/does not match HarvardX or MITx
str_detect(HXMITX$course_id, 'HarvardX')
str_detect(HXMITX$course_id, 'MITx')
# ----- b. Needed fields -----
# Create field "institution"
HXMITX$institution <- str_extract(HXMITX$course_id,"^\\w+(?=/)") ## Returns all before first /
# Create field "course_code"
HXMITX$course_code <- str_extract(HXMITX$course_id,"(?<=/).+(?=/)") ## Returns all between first and second /
# Create field "year_term"
HXMITX$year_term <- str_extract(HXMITX$course_id,"(?<=/)\\w+$") ## Returns all after second /
# Create field "year"
HXMITX$year <- str_extract(HXMITX$course_id,"(?<=/)\\d{4}") ## Returns all after second / and before _
# Create field "start_time_ym"
HXMITX$start_time_ym <- str_extract(HXMITX$start_time_DI,"^\\w+(?=-).+(?=-)") ## Returns all before second -
# Create field "last_event_ym"
HXMITX$last_event_ym <- str_extract(HXMITX$last_event_DI,"^\\w+(?=-).+(?=-)") ## Returns all before second -
# Create field "term"
HXMITX$term <- str_extract(HXMITX$course_id,"(?<=/\\d{4}_)\\w+$") ## Retuns all after _
# Create Letter Grade
HXMITX$letter_grade <- cut(HXMITX$grade,
breaks = c(0,.6,.7,.8,.9, 1.01),
labels = c("F", "D", "C", "B", "A"),
right = FALSE,
include.highest = TRUE,
include.lowest = TRUE)
# Create Indicator: nevents_ind
HXMITX$nevents_ind <- ifelse(HXMITX$nevents >= 1, 1,0)
# Create Indicator: ndays_act_ind
HXMITX$ndays_act_ind <- ifelse(HXMITX$ndays_act >= 1, 1,0)
# Create Indicator: nplay_video_ind
HXMITX$nplay_video_ind <- ifelse(HXMITX$nplay_video >= 1, 1,0)
# Create Indicator: nchapters_ind
HXMITX$nchapters_ind <- ifelse(HXMITX$nchapters >= 1, 1,0)
# Create Indicator: nforum_posts_ind
HXMITX$nforum_posts_ind <- ifelse(HXMITX$nforum_posts >= 1, 1,0)
# Create binary indicator for future modeling
set.seed(123)
HXMITX$treatment <- rbinom(n = 641138, size = 1, prob = 0.5)
# CONSIDER BINS FOR nevents, ndays_act, nplay_video, nchapters
# ===== Appending course summary data from second dataset =====
# Join datasets
HxMx <- left_join(HXMITX, courses, by = "course_code", copy = FALSE)
View(HxMx) # all rows
head(HxMx) # first few rows
tail(HxMx) # last few rows
summary(HxMx) # basic summary info
# Remove HXMITX and courses dataframes
rm(HXMITX)
rm(courses)
# Drop roles as all records are NULL
HxMx$roles <- NULL
# ----- Set Characters as Factors -----
# course_id -----
# Review distinct values for setting factors
unique(HxMx$course_id)
# Check if field is a factor
levels(HxMx$course_id)
# Change field to a factor
HxMx$course_id <- as.factor(HxMx$course_id) %>%
factor(levels = c(
"HarvardX/CB22x/2013_Spring",
"HarvardX/CS50x/2012",
"HarvardX/ER22x/2013_Spring",
"HarvardX/PH207x/2012_Fall",
"HarvardX/PH278x/2013_Spring",
"MITx/6.002x/2012_Fall",
"MITx/6.002x/2013_Spring",
"MITx/14.73x/2013_Spring",
"MITx/2.01x/2013_Spring",
"MITx/3.091x/2012_Fall",
"MITx/3.091x/2013_Spring",
"MITx/6.00x/2012_Fall",
"MITx/6.00x/2013_Spring",
"MITx/7.00x/2013_Spring",
"MITx/8.02x/2013_Spring",
"MITx/8.MReV/2013_Summer"
),
ordered = FALSE
)
# Ensure levels took and are accurate
levels(HxMx$course_id)
# final_cc_cname_DI -----
# Review distinct values for setting factors
unique(HxMx$final_cc_cname_DI)
# Check if field is a factor
levels(HxMx$final_cc_cname_DI)
# Change field to a factor
HxMx$final_cc_cname_DI <- as.factor(HxMx$final_cc_cname_DI) %>%
factor(levels = c(
"United States",
"France",
"Unknown/Other",
"Mexico",
"Australia",
"India",
"Canada",
"Russian Federation",
"Other South Asia",
"Other North & Central Amer., Caribbean",
"Other Europe",
"Other Oceania",
"Japan",
"Other Africa",
"Colombia",
"Germany",
"Other Middle East/Central Asia",
"Poland",
"Indonesia",
"Other East Asia",
"Bangladesh",
"China",
"United Kingdom",
"Ukraine",
"Spain",
"Greece",
"Pakistan",
"Brazil",
"Nigeria",
"Egypt",
"Other South America",
"Portugal",
"Philippines",
"Morocco"
),
ordered = FALSE
)
# Ensure levels took and are accurate
levels(HxMx$final_cc_cname_DI)
# LoE_DI -----
# Review distinct values for setting factors
unique(HxMx$LoE_DI)
# Check if field is a factor
levels(HxMx$LoE_DI)
# Change field to a factor
HxMx$LoE_DI <- as.factor(HxMx$LoE_DI) %>%
factor(levels = c(
"Less than Secondary",
"Secondary",
"Bachelor's",
"Master's",
"Doctorate"
),
ordered = TRUE
)
# Ensure levels took and are accurate
levels(HxMx$LoE_DI)
# Gender -----
# Review distinct values for setting factors
unique(HxMx$gender)
# Check if field is a factor
levels(HxMx$gender)
# Change field to a factor
HxMx$gender <- as.factor(HxMx$gender) %>%
factor(levels = c(
"m",
"f",
"o"
),
ordered = FALSE
)
# Ensure levels took and are accurate
levels(HxMx$gender)
# institution -----
# Review distinct values for setting factors
unique(HxMx$institution)
# Check if field is a factor
levels(HxMx$institution)
# Change field to a factor
HxMx$institution <- as.factor(HxMx$institution) %>%
factor(levels = c(
"HarvardX",
"MITx"
),
ordered = FALSE
)
# Ensure levels took and are accurate
levels(HxMx$institution)
# course_code -----
# Review distinct values for setting factors
unique(HxMx$course_code)
# Check if field is a factor
levels(HxMx$course_code)
# Change field to a factor
HxMx$course_code <- as.factor(HxMx$course_code) %>%
factor(levels = c(
"CB22x",
"CS50x",
"ER22x",
"PH207x",
"PH278x",
"6.002x",
"14.73x",
"2.01x",
"3.091x",
"6.00x",
"7.00x",
"8.02x",
"8.MReV"
),
ordered = FALSE
)
# Ensure levels took and are accurate
levels(HxMx$course_code)
# term -----
# Review distinct values for setting factors
unique(HxMx$term)
# Check if field is a factor
levels(HxMx$term)
# Change field to a factor
HxMx$term <- as.factor(HxMx$term) %>%
factor(levels = c(
"Fall",
"Spring",
"Summer"
),
ordered = FALSE
)
# Ensure levels took and are accurate
levels(HxMx$term)
# short_title -----
# Review distinct values for setting factors
unique(HxMx$short_title)
# Check if field is a factor
levels(HxMx$short_title)
# Change field to a factor
HxMx$short_title <- as.factor(HxMx$short_title) %>%
factor(levels = c(
"HeroesX",
"-",
"JusticeX",
"HealthStat",
"HealthEnv",
"Circuits",
"Poverty",
"Structures",
"SSChem",
"CS",
"Biology",
"E&M",
"MechRev"
),
ordered = FALSE
)
# Ensure levels took and are accurate
levels(HxMx$short_title)
# full_title -----
# Review distinct values for setting factors
unique(HxMx$full_title)
# Check if field is a factor
levels(HxMx$full_title)
# Change field to a factor
HxMx$full_title <- as.factor(HxMx$full_title) %>%
factor(levels = c(
"The Ancient Greek Hero",
"Introduction to Computer Science I",
"Justice",
"Health in Numbers: Quantitative Methods in Clinical & Public Health Research",
"Human Health and Global Environmental Change",
"Circuits and Electronics",
"The Challenges of Global Poverty",
"Elements of Structures",
"Introduction to Solid State Chemistry",
"Introduction to Computer Science and Programming",
"Introduction to Biology - The Secret of Life",
"Electricity and Magnetism",
"Mechanics Review"
),
ordered = FALSE
)
# Ensure levels took and are accurate
levels(HxMx$full_title)
# semester -----
# Review distinct values for setting factors
unique(HxMx$semester)
# Check if field is a factor
levels(HxMx$semester)
# Change field to a factor
HxMx$semester <- as.factor(HxMx$semester) %>%
factor(levels = c(
"Spring-Summer 2013",
"Fall 2012 - Spring 2013",
"Fall 2012",
"Summer 2013",
"Fall 2012 and Spring 2013",
"Spring 2013"
),
ordered = FALSE
)
# Ensure levels took and are accurate
levels(HxMx$semester)
# ----- Set Characters as Numeric -----
class(HxMx$YoB) # Check class
HxMx$YoB <- as.integer(HxMx$YoB)
class(HxMx$YoB) # Check new class
# ===== Write HxMx to .rds for recall after refreshing session =====
# write_csv(HxMx, "HxMx.csv") -- removed to save as .rds instead
saveRDS(HxMx, "HxMx.rds") # saves factors in file
# ===== Drop test and created fields =====
#HXMITX$institution <- NULL
#HXMITX$course <- NULL
#HXMITX$year_term <- NULL
#HXMITX$year <- NULL
#HXMITX$term <- NULL
#HXMITX$HarvardX <- NULL
#HXMITX$MITx <- NULL
#HXMITX$harvardx <- NULL
#HXMITX$mitx <- NULL
#HXMITX$letter_grade <- NULL
#HXMITX$nevents_ind <- NULL
#HXMITX$ndays_act_ind <- NULL
#HXMITX$nplay_video_ind <- NULL
#HXMITX$nchapters_ind <- NULL
#HXMITX$nforum_posts_ind <- NULL