-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathWeek2_worksheet.R
More file actions
285 lines (170 loc) · 5.88 KB
/
Week2_worksheet.R
File metadata and controls
285 lines (170 loc) · 5.88 KB
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
library(data.table)
library(reshape2)
library(extraoperators)
### 1. Vectors ###
## First let's remember how we create vectors
## Assign a vector with the numbers 8, 3, 5, and 6 to an object called v
???
## view my_vector
v
## How would you add 2 to every element of v?
???
### 2. Loading datasets ###
## Let's load the aces_daily dataset, which is built into a package.
## Try loading aces_daily now.
## Hint: What do we need to do first before accessing it?
???
## Hint 2: load it, then change it to a data table
???
???
### 3. Subsetting and Operators ###
## Try making a new dataset from aces_daily that only includes
## observations from people who were born in Australia
d_aus <- ???
## Now make a new dataset of observations that are only "medium" amounts
## (i.e., not too high or not too low) of STRESS. You get to decide what
## counts as "medium".
d_medstress <-???
## select only low stress (STRESS less than 2) observations from people
## who were born in Australia
d_aus_lowstress <- ???
## Make your own subset of observations based on any rules,
## using an operator you haven't tried yet
???
## Try a big one - Subsetting
# Remember that we use ! and is.na for not missing
# Remember that we can create a new column called count and assign it a sample size
# Remember that we can chain operations
## Select observations where:
# - positive affect, in the dataset, called `PosAff` is not missing
# - participants who have **at least 20** non missing positive affect (any value)
# - where positive affect scores are greater than or equal to 3
# You should end up with 2,455 observations.
d3 <- ???
d3[, .(UserID, PosAff)]
### 4. Dates ###
## Now you try to convert the following character strings into date class
## objects in `R` by filling in the correct template in quotes for the
## `format = ` argument. If you do it incorrectly, you will get errors or
## `NA` indicating the text could not be converted to a date using the
## template you supplied.
y1 <- c("5/28/2018", "6/30/2019")
??? ## you complete
y2 <- c("28 01 2018", "15 06 2019")
dy2 <- ??? ## you complete
dy2 ## show results
y3 <- c("28-1-18", "15-6-19")
??? ## you complete
## how many days are there between
## 28 January 2018 and 15 June 2019?
## use the object dy2 to complete like
## we did above
???
## Subset Dates
## try to create a dataset that excludes any weekend days.
## If you have forgotten what the weekdays() function does,
## try > ?weekdays in the console
## Hint: You may need to review your list of operators
d4 <- ???
## Check: there should now be no surveys on weekend days
table(weekdays(d4$SurveyDay))
### 5. Factors ###
## Now try using factors for dummy coding. In aces_daily,
## there is a variable, SurveyInteger. 1 = morning, 2 = afternoon,
## 3 = evening. Make it a factor and then create a frequency table
## using `table()`.
## Hint: Use the := operator to create a new column called "SurveyIntegerF"
???
table(d$SurveyIntegerF)
### 6. Merging ###
## Here are two datasets shown as tables.
ds1 <- data.table(
ID = c("A1", "B2", "C3"),
Stress = c(4, 5, 6))
ds1
ds2 <- data.table(
ID = c("A1", "B2", "D4", "E5"),
Steps = c(9524, 15303, 7432, 4035))
ds2
# Before you try joining those datasets, try to answer these
# questions, which are similar to what might be on the exam.
# For these questions, `ds1` is the "left"
# dataset and `ds2` is the "right" dataset and you are joining by ID.
# Q: How many rows would occur in a **natural join** of `ds1` and
# `ds2`? How many missing values total across all
# variables?
# A:
#
#
#
#
#
# Q: How many rows would occur in a **full outer join** of `ds1` and
# `ds2`? How many missing values total across all
# variables?
# A:
#
#
#
#
#
# Q: How many rows would occur in a **left outer join** of `ds1` and
# `ds2`? How many missing values total across all variables?
# A:
# Q: How many rows would occur in a **right outer join** of `ds1` and
# `ds2`? How many missing values total across all variables?
# A:
## using the datasets ds1 and ds2, do a natural join
???
## using the datasets ds1 and ds2, do a full outer join
???
## using the datasets ds1 and ds2, do a left outer join
???
## using the datasets ds1 and ds2, do a right outer join
???
### 7. Duplicates ###
## Using aces_daily:
## Count how many unique IDs there are
length(???)
## Create a frequency table of the number of times `UserID` is
## duplicated.
table(???)
## Check if there are duplicates if you use both `UserID` and
## `SurveyDay` to create a combined ID.
## Hint: Use subsetting and the c() function
???
## Check if there are duplicates if you use `UserID` and `SurveyDay`
## and `SurveyInteger`.
???
### 8. Reshape ###
## Using this dataset, you try reshaping a WIDE dataset into a LONG format.
dwtry <- data.table(
Name = c("Jane", "John"),
SLEEP1 = c(2, 1),
SLEEP2 = c(4, 5),
Dep1 = c(4, 5),
Dep2 = c(3, 2),
Dep3 = c(5, 5))
# Hint: How many time points do we have for each variable?
dwtry[???]
dltry <- ???
print(dltry)
## Now reshape `dltry` from LONG back to WIDE.
???
### 9. Merging by Multiple IDs ###
## Let's say that one participant had technical problems with the surveys
## from the daily data we have worked with and so filled out the last few
## on paper and pencil. Now we want to merge this data into our master
## daily dataset, `d` (i.e., aces_daily), using a *full outer join*.
## The resulting dataset, `d6` should have 6,602 rows.
pencil <- data.table(
UserID = 1L,
SurveyDay = as.Date("2017-3-08"),
SurveyIntegerF = as.factor(c("Morning", "Afternoon", "Evening")),
STRESS = c(1, 3, 1))
## Now merge into a new object called d6
d6 <- ???
## How many of you ended up with two STRESS (.x and .y) variables in d6?
## This happens when two data tables share a variable that isn't dealt with
## How can you fix this?
d6 <- ???