-
Notifications
You must be signed in to change notification settings - Fork 0
/
02-wrangling.qmd
870 lines (525 loc) · 30.8 KB
/
02-wrangling.qmd
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
# Data wrangling I {#sec-wrangling}
## Intended Learning Outcomes {.unnumbered}
In the next two chapters, we will build on the data wrangling skills from level 1. We will revisit all the functions you have already encountered (and might have forgotten over the summer break) and introduce 2 or 3 new functions. These two chapters will provide an opportunity to revise and apply the functions to a novel dataset.
By the end of this chapter, you should be able to:
- apply familiar data wrangling functions to novel datasets
- read and interpret error messages
- realise there are several ways of getting to the results
- export data objects as csv files
The main purpose of this chapter and @sec-wrangling2 is to wrangle your data into shape for data visualisation (@sec-dataviz and @sec-dataviz2). For the two chapters, we will:
1. calculate demographics
2. tidy 3 different questionnaires with varying degrees of complexity
3. solve an error mode problem
4. join all data objects together
## [Individual Walkthrough]{style="color: #F39C12; text-transform: uppercase;"} {.unnumbered}
Before we start, we need to set up some things.
## Activity 1: Setup
* We will be working on the **dataset by Pownall et al. (2023)** again, which means we can still use the project we created last week. The data files will already be there, so no need to download them again.
* To **open the project** in RStudio, go to the folder in which you stored the project and the data last time, and double click on the project icon.
* **Create a new `.Rmd` file** for chapter 2 and save it to your project folder. Name it something meaningful (e.g., “chapter_02.Rmd”, “02_data_wrangling.Rmd”). See @sec-rmd if you need some guidance.
* In your newly created `.Rmd` file, delete everything below line 12 (after the set-up code chunk).
## Activity 2: Load in the libraries and read in the data
We will use `tidyverse` today, and we want to create a data object `data_prp` that stores the data from the file `prp_data_reduced.csv`.
::: {.callout-note collapse="true" icon="false"}
## Hint
```{r eval=FALSE}
library(???)
data_prp <- read_csv("???")
```
```{r include=FALSE, message=TRUE}
## I basically have to have 2 code chunks since I tell them to put the data files next to the project, and mine are in a separate folder called data - unless I'll turn this into a fixed path
library(tidyverse)
data_prp <- read_csv("data/prp_data_reduced.csv")
```
:::
::: {.callout-caution collapse="true" icon="false"}
## Solution
```{r eval=FALSE}
library(tidyverse)
data_prp <- read_csv("prp_data_reduced.csv")
```
:::
If you need a quick reminder what the dataset was about, have a look at the abstract in @sec-download_data_ch1. We also addressed the changes we made to the dataset there.
And remember to have a quick `glimpse()` at your data.
## Activity 3: Calculating demographics
Let’s start with some simple data-wrangling steps to compute demographics for our original dataset, `data_prp`. First, we want to determine how many participants took part in the study by Pownall et al. (2023) and compute the mean age and the standard deviation of age for the sample.
### ... for the full sample using `summarise()`
The `summarise()` function is part of the **"Wickham Six"** alongside `group_by()`, `select()`, `filter()`, `mutate()`, and `arrange()`. You used them plenty of times last year.
Within `summarise()`, we can use the `n()` function, which calculates the number of rows in the dataset. Since each row corresponds to a unique participant, this gives us the total number of participants.
To calculate the mean age and the standard deviation of age, we need to use the functions `mean()` and `sd()` on the column `Age` respectively.
```{r, error=TRUE}
demo_total <- data_prp %>%
summarise(n = n(), # participant number
mean_age = mean(Age), # mean age
sd_age = sd(Age)) # standard deviation of age
demo_total
```
R did not give us an error message per se, but the output is not quite as expected either. There are `NA` values in the `mean_age` and `sd_age` columns. Looking at the warning message and at `Age`, can you explain what happened?
::: {.callout-caution collapse="true" icon="false"}
## Answer
The warning message says: `argument is not numeric or logical: returning NA` If we look at the `Age` column more closely, we can see that it's a character data type.
:::
#### Fixing `Age` {.unnumbered}
Might be wise to look at the unique answers in column `Age` to determine what is wrong. We can do that with the function `distinct()`.
```{r results='hide'}
age_distinct <- data_prp %>%
distinct(Age)
age_distinct
```
::: {.callout-caution collapse="true" icon="false"}
## Show the unique values of `Age`.
```{r echo=FALSE}
age_distinct
```
:::
::: columns
::: column
One cell has the string "years" added to their number 25, which has converted the entire column into a character column.
We can easily fix this by extracting only the numbers from the column and converting it into a numeric data type. The `parse_number()` function, which is part of the `tidyverse` package, handles both steps in one go (so there’s no need to load additional packages).
We will combine this with the `mutate()` function to create a new column called `Age` (containing those numeric values), effectively replacing the old `Age` column (which had the character values).
:::
::: column
![parse_number() illustration by Allison Horst (see [https://allisonhorst.com/r-packages-functions](https://allisonhorst.com/r-packages-functions){target="_blank"})](images/parse_number.png){width="95%"}
:::
:::
```{r}
data_prp <- data_prp %>%
mutate(Age = parse_number(Age))
typeof(data_prp$Age) # fixed
```
#### Computing summary stats {.unnumbered}
Excellent. Now that the numbers are in a numeric format, let's try calculating the demographics for the total sample again.
```{r}
demo_total <- data_prp %>%
summarise(n = n(), # participant number
mean_age = mean(Age), # mean age
sd_age = sd(Age)) # standard deviation of age
demo_total
```
Even though there's no error or warning, the table still shows `NA` values for `mean_age` and `sd_age`. So, what could possibly be wrong now?
::: {.callout-caution collapse="true" icon="false"}
## Answer
Did you notice that the `Age` column in `age_distinct` contains some missing values (`NA`)? To be honest, it's easier to spot this issue in the actual R output than in the printed HTML page.
:::
#### Computing summary stats - third attempt {.unnumbered}
To ensure R ignores missing values during calculations, we need to add the extra argument `na.rm = TRUE` to the `mean()` and `sd()` functions.
```{r}
demo_total <- data_prp %>%
summarise(n = n(), # participant number
mean_age = mean(Age, na.rm = TRUE), # mean age
sd_age = sd(Age, na.rm = TRUE)) # standard deviation of age
demo_total
```
Finally, we’ve got it! 🥳 Third time's the charm!
### ... per gender using `summarise()` and `group_by()`
Now we want to compute the summary statistics for each gender. The code inside the `summarise()` function remains unchanged; we just need to use the `group_by()` function beforehand to tell R that we want to compute the summary statistics for each group separately. It’s also a good practice to use `ungroup()` afterwards, so you are not taking groupings forward unintentionally.
```{r}
demo_by_gender <- data_prp %>%
group_by(Gender) %>% # split data up into groups (here Gender)
summarise(n = n(), # participant number
mean_age = mean(Age, na.rm = TRUE), # mean age
sd_age = sd(Age, na.rm = TRUE)) %>% # standard deviation of age
ungroup()
demo_by_gender
```
### Adding percentages
Sometimes, it may be useful to calculate percentages, such as for the gender split. You can do this by adding a line within the `summarise()` function to perform the calculation. All we need to do is take the number of female, male, and non-binary participants (stored in the `n` column of `demo_by_gender`), divide it by the total number of participants (stored in the `n` column of `demo_total`), and multiply by 100. Let's add `percentage` to the `summarise()` function of `demo_by_gender`. Make sure that the code for `percentages` is placed after the value for `n` has been computed.
Accessing the value of `n` for the different gender categories is straightforward because we can refer back to it directly. However, since the total number of participants is stored in a different data object, we need to use a base R function to access it – specifically the `$` operator. To do this, you simply type the name of the data object (in this case, `demo_total`), followed by the `$` symbol (with no spaces), and then the name of the column you want to retrieve (in this case, `n`). The general pattern is `data$column`.
```{r}
demo_by_gender <- data_prp %>%
group_by(Gender) %>%
summarise(n = n(),
# n from the line above divided by n from demo_total *100
percentage = n/demo_total$n *100,
mean_age = mean(Age, na.rm = TRUE),
sd_age = sd(Age, na.rm = TRUE)) %>%
ungroup()
demo_by_gender
```
::: {.callout-tip collapse="true"}
## Tip for decimal places - use `round()`
Not super important, because you could round the values by yourself when writing up your reports, but if you wanted to tidy up the decimal places in the output, you can do that using the `round()` function. You would need to "wrap" it around your computations and specify how many decimal places you want to display (for example `mean(Age)` would turn into `round(mean(Age), 1)`). It may look odd for `percentage`, just make sure the number that specifies the decimal places is placed **within** the round function. The default value is 0 (meaning no decimal spaces).
```{r}
demo_by_gender <- data_prp %>%
group_by(Gender) %>%
summarise(n = n(),
percentage = round(n/demo_total$n *100, 2), # percentage with 2 decimal places
mean_age = round(mean(Age, na.rm = TRUE), 1), # mean Age with 1 decimal place
sd_age = round(sd(Age, na.rm = TRUE), 3)) %>% # sd Age with 3 decimal places
ungroup()
demo_by_gender
```
:::
## Activity 4: Questionable Research Practices (QRPs) {#sec-ch2_act4}
#### The main goal is to compute the mean QRP score per participant for time point 1. {.unnumbered}
At the moment, the data is in wide format. The table below shows data from the first 3 participants:
```{r}
head(data_prp, n = 3)
```
<p></p>
Looking at the QRP data at time point 1, you determine that
* individual item columns are `r mcq(c(answer = "numeric", x = "character"))`, and
* according to the codebook, there are `r mcq(c(answer = "no", x = "some"))` reverse-coded items in this questionnaire.
According to the codebook and the data table above, we just have to **compute the average score for QRP items `r fitb("1")` to `r fitb("11")`**, since items `r fitb("12")` to `r fitb("15")` are distractor items. Seems quite straightforward.
However, as you can see in the table above, each item is in a separate column, meaning the data is in **wide format**. It would be much easier to calculate the mean scores if the items were arranged in **long format**.
Let’s tackle this problem step by step. It’s best to create a separate data object for this. If we tried to compute it within `data_prp`, it could quickly become messy.
* **Step 1**: Select the relevant columns `Code`, and `QRPs_1_Time1` to `QRPs_11_Time1` and store them in an object called `qrp_t1`
* **Step 2**: Pivot the data from wide format to long format using `pivot_longer()` so we can calculate the average score more easily (in step 3)
* **Step 3**: Calculate the average QRP score (`QRPs_Acceptance_Time1_mean`) per participant using `group_by()` and `summarise()`
```{r qrps}
qrp_t1 <- data_prp %>%
#Step 1
select(Code, QRPs_1_Time1:QRPs_11_Time1) %>%
# Step 2
pivot_longer(cols = -Code, names_to = "Items", values_to = "Scores") %>%
# Step 3
group_by(Code) %>% # grouping by participant id
summarise(QRPs_Acceptance_Time1_mean = mean(Scores)) %>% # calculating the average Score
ungroup() # just make it a habit
```
::: {.callout-caution icon="false" collapse="true"}
## Explain the individual functions
::: panel-tabset
## `select ()`
The select function allows to include or exclude certain variables (columns). Here we want to focus on the participant ID column (i.e., `Code`) and the QRP items at time point 1. We can either list them all individually, i.e., Code, QRPs_1_Time1, QRPs_2_Time1, QRPs_3_Time1, and so forth (you get the gist), but that would take forever to type.
A shortcut is to use the colon operator `:`. It allows us to select all columns that fall within the range of `first_column_name` to `last_column_name`. We can apply this here since the QRP items (1 to 11) are sequentially listed in `data_prp`.
```{r}
qrp_step1 <- data_prp %>%
select(Code, QRPs_1_Time1:QRPs_11_Time1)
# show first 5 rows of qrp_step1
head(qrp_step1, n = 5)
```
How many rows/observations and columns/variables do we have in `qrp_step1`?
* rows/observations: `r fitb("89")`
* columns/variables: `r fitb("12")`
## `pivot_longer()`
As you can see, the table we got from Step 1 is in wide format. To get it into wide format, we need to define:
* the columns that need to be reshuffled from wide into long format (`col` argument). Here we selected "everything except the `Code` column", as indicated by `-Code` \[minus `Code`\]. However, `QRPs_1_Time1:QRPs_11_Time1` would also work and give you the exact same result.
* the `names_to` argument. R is creating a new column in which all the column names from the columns you selected in `col` will be stored in. Here we are naming this column "Items" but you could pick something equally sensible if you like.
* the `values_to` argument. R creates this second column to store all responses the participants gave to the individual questions, i.e., all the numbers in this case. We named it "Scores" here, but you could have called it something different, like "Responses"
```{r}
qrp_step2 <- qrp_step1 %>%
pivot_longer(cols = -Code, names_to = "Items", values_to = "Scores")
# show first 15 rows of qrp_step2
head(qrp_step2, n = 15)
```
Now, have a look at `qrp_step2`. In total, we now have `r fitb("979")` rows/observations, `r fitb("11")` per participant, and `r fitb("3")` columns/variables.
## `group_by()` and `summarise()`
This follows exactly the same sequence we used when calculating descriptive statistics by gender. The only difference is that we are now grouping the data by the participant's `Code` instead of `Gender`.
`summarise()` works exactly the same way: `summarise(new_column_name = function_to_calculate_something(column_name_of_numeric_values))`
The `function_to_calculate_something` can be `mean()`, `sd()` or `sum()` for mean scores, standard deviations, or summed-up scores respectively. You could also use `min()` or `max()` if you wanted to determine the lowest or the highest score for each participant.
:::
:::
::: callout-tip
You could **rename the columns whilst selecting** them. The pattern would be `select(new_name = old_name)`. For example, if we wanted to select variable `Code` and rename it as `Participant_ID`, we could do that.
```{r}
renaming_col <- data_prp %>%
select(Participant_ID = Code)
head(renaming_col, n = 5)
```
:::
## Activity 5: Knitting
Once you've completed your R Markdown file, the final step is to "knit" it, which converts the `.Rmd` file into a HTML file. Knitting combines your code, text, and output (like tables and plots) into a single cohesive document. This is a really good way to check your code is working.
To knit the file, **click the Knit button** at the top of your RStudio window. The document will be generated and, depending on your setting, automatically opened in the viewer in the `Output pane` or an external browser window.
If any errors occur during knitting, RStudio will show you an error message with details to help you troubleshoot.
If you want to **intentionally keep any errors** we tackled today to keep a reference on how you solved them, you could add `error=TRUE` or `eval=FALSE` to the code chunk that isn't running.
## Activity 6: Export a data object as a csv
To avoid having to repeat the same steps in the next chapter, it's a good idea to save the data objects you've created today as csv files. You can do this by using the `write_csv()` function from the `readr` package. The csv files will appear in your project folder.
The basic syntax is:
```{r eval=FALSE}
write_csv(data_object, "filename.csv")
```
Now, let's export the objects `data_prp` and `qrp_t1`.
```{r eval=FALSE}
write_csv(data_prp, "data_prp_for_ch3.csv")
```
Here we named the file `data_prp_for_ch3.csv`, so we wouldn't override the original data csv file `prp_data_reduced.csv`. However, feel free to choose a name that makes sense to you.
::: {.callout-note icon="false"}
## Your Turn
Export `qrp_t1`.
::: {.callout-caution collapse="true" icon="false"}
## Solution
```{r eval=FALSE}
write_csv(qrp_t1, "qrp_t1.csv")
```
:::
:::
Check that your csv files have appeared in your project folder, and you're all set!
**That’s it for Chapter 2: Individual Walkthrough.**
## [Pair-coding]{style="color: #F39C12; text-transform: uppercase;"} {.unnumbered}
```{r reading in data for me, echo=FALSE, message=FALSE}
library(tidyverse)
dog_data_raw <- read_csv("data/dog_data_raw.csv")
```
We will continue working with the data from Binfet et al. (2021), focusing on the randomised controlled trial of therapy dog interventions. Today, our goal is to **calculate an average `Flourishing` score for each participant** at time point 1 (pre-intervention) using the raw data file `dog_data_raw`. Currently, the data looks like this:
```{r echo=FALSE}
flourishing_brief <- dog_data_raw %>%
select(RID, F1_1:F1_8)
head(flourishing_brief, n = 5)
```
However, we want the data to look like this:
```{r echo=FALSE}
flourishing_tidy <- dog_data_raw %>%
select(RID, starts_with("F1")) %>%
pivot_longer(cols = -RID, names_to = "Names", values_to = "Responses") %>%
group_by(RID) %>%
summarise(Flourishing_pre = mean(Responses)) %>%
ungroup()
head(flourishing_tidy, n = 5)
```
### Task 1: Open the R project you created last week {.unnumbered}
If you haven’t created an R project for the lab yet, please do so now. If you already have one set up, go ahead and open it.
### Task 2: Open your `.Rmd` file from last week {.unnumbered}
Since we haven’t used it much yet, feel free to continue using the `.Rmd` file you created last week in Task 2.
### Task 3: Load in the library and read in the data {.unnumbered}
The data should be in your project folder. If you didn’t download it last week, or if you’d like a fresh copy, you can download the data again here: [data_pair_coding](data/data_pair_coding.zip "download").
We will be using the `tidyverse` package today, and the data file we need to read in is `dog_data_raw.csv`.
::: {.callout-note collapse="true" icon="false"}
## Hint
```{r eval=FALSE}
# loading tidyverse into the library
library(???)
# reading in `dog_data_raw.csv`
dog_data_raw <- read_csv("???")
```
:::
### Task 4: Calculating the mean for `Flourishing_pre` {.unnumbered}
* **Step 1**: Select all relevant columns from `dog_data_raw`, including participant ID and all items from the `Flourishing` questionnaire completed before the intervention. Store this data in an object called `data_flourishing`.
::: {.callout-note collapse="true" icon="false"}
## Hint
Look at the codebook. Try to determine:
* The variable name of the column where the participant ID is stored.
* The items related to the Flourishing scale at the pre-intervention stage.
::: {.callout-note collapse="true" icon="false"}
## More concrete hint
From the codebook, we know that:
* The participant ID column is called `RID`.
* The Flourishing items at the pre-intervention stage start with `F1_`.
```{r eval=FALSE}
data_flourishing <- ??? %>%
select(???, F1_???:F1_???)
```
:::
:::
* **Step 2**: Pivot the data from wide format to long format so we can calculate the average score more easily (in step 3).
::: {.callout-note collapse="true" icon="false"}
## Hint
Which pivot function should you use? We have `pivot_wider()` and `pivot_longer()` to choose from.
We also need 3 arguments in that function:
* The columns you want to select (e.g., all the Flourishing items),
* The name of the column where the current column headings will be stored (e.g., "Questionnaire"),
* The name of the column that should store all the values (e.g., "Responses").
::: {.callout-note collapse="true" icon="false"}
## More concrete hint
We need `pivot_longer()`. You already encountered `pivot_longer()` in first year (or in the individual walkthrough if you have already completed this Chapter). The 3 arguments was also a give-away; `pivot_wider()` only requires 2 arguments.
```{r eval=FALSE}
pivot_longer(cols = ???, names_to = "???", values_to = "???")
```
:::
:::
* **Step 3**: Calculate the average Flourishing score per participant and name this column `Flourishing_pre` to match the table above.
::: {.callout-note collapse="true" icon="false"}
## Hint
Before summarising the mean, you may need to group the data.
::: {.callout-note collapse="true" icon="false"}
## More concrete hint
To compute an average score **per participant**, we would need to group by participant ID first.
```{r eval=FALSE}
group_by(???) %>%
summarise(Flourishing_pre = mean(???)) %>%
ungroup()
```
:::
:::
::: {.callout-caution collapse="true" icon="false"}
## Solution
```{r eval=FALSE}
# loading tidyverse into the library
library(tidyverse)
# reading in `dog_data_raw.csv`
dog_data_raw <- read_csv("dog_data_raw.csv")
# Task 4: Tidying
data_flourishing <- dog_data_raw %>%
# Step 1
select(RID, F1_1:F1_8) %>%
# Step 2
pivot_longer(cols = -RID, names_to = "Questionnaire", values_to = "Responses") %>%
# Step 3
group_by(RID) %>%
summarise(Flourishing_pre = mean(Responses)) %>%
ungroup()
```
:::
## [Test your knowledge and challenge yourself]{style="color: #F39C12; text-transform: uppercase;"} {.unnumbered}
### Knowledge check {.unnumbered}
#### Question 1 {.unnumbered}
Which function of the Wickham Six would you use to include or exclude certain variables (columns)? `r mcq(c(answer = "select()", x = "filter()", x = "mutate()", x = "arrange()", x = "group_by()", x = "summarise()"))`
#### Question 2 {.unnumbered}
Which function of the Wickham Six would you use to create new columns or modify existing columns in a dataframe? `r mcq(c(x = "select()", x = "filter()", answer = "mutate()", x = "arrange()", x = "group_by()", x = "summarise()"))`
#### Question 3 {.unnumbered}
Which function of the Wickham Six would you use to organise data into groups based on one or more columns? `r mcq(c(x = "select()", x = "filter()", x = "mutate()", x = "arrange()", answer = "group_by()", x = "summarise()"))`
#### Question 4 {.unnumbered}
Which function of the Wickham Six would you use to sort the rows of a dataframe based on the values in one or more columns? `r mcq(c(x = "select()", x = "filter()", x = "mutate()", answer = "arrange()", x = "group_by()", x = "summarise()"))`
#### Question 5 {.unnumbered}
Which function of the Wickham Six would NOT modify the original dataframe? `r mcq(c(x = "select()", x = "filter()", x = "mutate()", x = "arrange()", x = "group_by()", answer = "summarise()"))`
::: {.callout-caution collapse="true" icon="false"}
## Explain these answers
| Function | Description |
|:-------------|:------------------------------------------------------|
| `select()` | Include or exclude certain variables/columns |
| `filter()` | Include or exclude certain observations/rows |
| `mutate()` | Creates new columns or modifies existing ones |
| `arrange()` | Changes the order of the rows |
| `group_by()` | Split data into groups based on one or more variables |
| `summarise()`| Creates a new dataframe returning one row for each combination of grouping variables |
Technically, the first five functions operate on the existing data object, making adjustments like sorting the data (e.g., with `arrange()`), reducing the number of rows (e.g., with `filter()`), reducing the number of columns (e.g., with `select()`), or adding new columns (e.g., with `mutate()`). In contrast, `summarise()` fundamentally alters the structure of the original dataframe by generating a completely new dataframe that contains only summary statistics, rather than retaining the original rows and columns.
:::
### Error mode {.unnumbered}
Some of the code chunks contain mistakes and result in errors, while others do not produce the expected results. Your task is to identify any issues, explain why they occurred, and, if possible, fix them.
We will use a few built-in datasets, such as `billboard` and `starwars`, to help you replicate the errors in your own R environment. You can view the data either by typing the dataset name directly into your console or by storing the data as a separate object in your `Global Environment`.
```{r eval=FALSE}
billboard
starwars_data = starwars
```
#### Question 6 {.unnumbered}
Currently, the weekly song rankings for Billboard Top 100 in 2000 are in wide format, with each week in a separate column. The following code is supposed to transpose the wide-format `billboard` data into long format:
```{r error=TRUE}
long_data <- billboard %>%
pivot_longer(names_to = "weeks", values_to = "rank")
```
What does this error message mean and how do you fix it?
::: {.callout-caution collapse="true" icon="false"}
## Explain the solution
The error message indicates that the `cols` argument is missing in the function. This means the function doesn’t know which columns to transpose from wide format to long format.
FIX: Add `cols = wk1:wk76` to the function to select columns from wk1 to wk76. Alternatively, `cols = starts_with("wk")` would also work since all columns start with the letter combination "wk".
```{r}
long_data <- billboard %>%
pivot_longer(cols = wk1:wk76, names_to = "weeks", values_to = "rank")
# OR
long_data <- billboard %>%
pivot_longer(cols = starts_with("wk"), names_to = "weeks", values_to = "rank")
```
:::
#### Question 7 {.unnumbered}
The following code is intended to calculate the mean height of all the characters in the built-in `starwars` dataset, grouped by their gender.
```{r error=TRUE, message=FALSE}
summary_data <- starwars %>%
group_by(gender) %>%
summarise(mean_height = height)
```
The code runs, but it's giving us some weird warning and the output is also not as expected. What steps should we take to fix this?
::: {.callout-caution collapse="true" icon="false"}
## Explain the solution
The aggregation function `mean()` is missing from within `summarise()`. Without it, the function does not perform any aggregation and returns *all* rows with only the columns for gender and height.
FIX: Wrap the `mean()` function around the variable you want to aggregate, here `height`.
```{r}
summary_data <- starwars %>%
group_by(gender) %>%
summarise(mean_height = mean(height))
```
:::
#### Question 8 {.unnumbered}
Following up on Question 7, we now have `summary_data` that looks approximately correct - it has the expected rows and column numbers, however, the cell values are "weird".
```{r}
summary_data
```
Can you explain what is happening here? And how can we modify the code to fix this?
::: {.callout-caution collapse="true" icon="false"}
## Explain the solution
Look at the original `starwars` data. You will notice that some of the characters with feminine and masculine gender entries have missing height values. However, all four characters without a specified gender have provided their height.
FIX: We need to add `na.rm = TRUE` to the `mean()` function to ensure that R ignores missing values before aggregating the data.
```{r}
summary_data <- starwars %>%
group_by(gender) %>%
summarise(mean_height = mean(height, na.rm = TRUE))
summary_data
```
:::
### Challenge yourself {.unnumbered}
If you want to **challenge yourself** and further apply the skills from Chapter 2, you can wrangle the data from `dog_data_raw` for additional questionnaires from either the pre- and/or post-intervention stages:
* Calculate the mean score for `flourishing_post` for each participant.
* Calculate the mean score for the `PANAS` (Positive and/or Negative Affect) per participant
* Calculate the mean score for happiness (`SHS`) per participant
The 3 steps are equivalent for those questionnaires - select, pivot, group_by and summarise; you just have to "replace" the questionnaire items involved.
::: {.callout-caution collapse="true" icon="false"}
## Solution for **Challenge yourself**
Flourishing post-intervention
```{r eval=FALSE}
## flourishing_post
flourishing_post <- dog_data_raw %>%
# Step 1
select(RID, starts_with("F2")) %>%
# Step 2
pivot_longer(cols = -RID, names_to = "Names", values_to = "Response") %>%
# Step 3
group_by(RID) %>%
summarise(Flourishing_post = mean(Response)) %>%
ungroup()
```
The PANAS could be solved more concisely with the skills we learn in @sec-wrangling2, but for now, you would have solved it this way:
```{r eval=FALSE}
# PANAS - positive affect pre
PANAS_PA_pre <- dog_data_raw %>%
# Step 1
select(RID, PN1_3, PN1_5, PN1_7, PN1_8, PN1_10) %>%
# Step 2
pivot_longer(cols = -RID, names_to = "Items", values_to = "Scores") %>%
# Step 3
group_by(RID) %>%
summarise(PANAS_PA_pre = mean(Scores)) %>%
ungroup()
# PANAS - positive affect post
PANAS_PA_post <- dog_data_raw %>%
# Step 1
select(RID, PN2_3, PN2_5, PN2_7, PN2_8, PN2_10) %>%
# Step 2
pivot_longer(cols = -RID, names_to = "Items", values_to = "Scores") %>%
# Step 3
group_by(RID) %>%
summarise(PANAS_PA_post = mean(Scores)) %>%
ungroup()
# PANAS - negative affect pre
PANAS_NA_pre <- dog_data_raw %>%
# Step 1
select(RID, PN1_1, PN1_2, PN1_4, PN1_6, PN1_9) %>%
# Step 2
pivot_longer(cols = -RID, names_to = "Items", values_to = "Scores") %>%
# Step 3
group_by(RID) %>%
summarise(PANAS_NA_pre = mean(Scores)) %>%
ungroup()
# PANAS - negative affect post
PANAS_NA_post <- dog_data_raw %>%
# Step 1
select(RID, PN2_1, PN2_2, PN2_4, PN2_6, PN2_9) %>%
# Step 2
pivot_longer(cols = -RID, names_to = "Items", values_to = "Scores") %>%
# Step 3
group_by(RID) %>%
summarise(PANAS_NA_post = mean(Scores)) %>%
ungroup()
```
Happiness scale
```{r eval=FALSE}
# happiness_pre
happiness_pre <- dog_data_raw %>%
# Step 1
select(RID, HA1_1, HA1_2, HA1_3) %>%
# Step 2
pivot_longer(cols = -RID, names_to = "Item", values_to = "Score") %>%
# Step 3
group_by(RID) %>%
summarise(SHS_pre = mean(Score)) %>%
ungroup()
#happiness_post
happiness_post <- dog_data_raw %>%
# Step 1
select(RID, HA2_1, HA2_2, HA2_3) %>%
# Step 2
pivot_longer(cols = -RID, names_to = "Item", values_to = "Score") %>%
# Step 3
group_by(RID) %>%
summarise(SHS_post = mean(Score)) %>%
ungroup()
```
:::