-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy path02 tidy data.R
96 lines (56 loc) · 2.09 KB
/
02 tidy data.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
library(tidyverse)
data_raw <- read.csv("data/deaf_numer.csv")
data_clean <- data_raw |>
select(sID, nFingers, rt) |>
filter(rt < 2500)
# Join data frames --------------------------------------------------------
# Sometimes our data is split across more than one data frame. Sometimes examples:
# - Data from different sessions is stored in different files
# - Dyad data is split (mother data in one file, baby in another).
# - Data from different questionnaires / tasks is stored in separate files.
# For example, we have:
# 1. A file with performance on some task.
head(data_clean)
# 2. A file with demographic info for each subject.
subject_info <- read.csv("data/deaf_numer_sinfo.csv")
head(subject_info)
# We can easily JOIN these data according to some key - the subject.
data_clean_joined <- data_clean |>
full_join(subject_info, by = "sID")
head(data_clean_joined, n = 12)
# Reshaping data ----------------------------------------------------------
# Most modeling functions in R take a data frame that is tidy. Unfortunately,
# this is not true for other unmentionable statistical programs...
emotionalWM <- readxl::read_excel("data/Zhang2018_emotionalWM.xlsx")
# data from https://doi.org/10.3389%2Ffnbeh.2018.00065
head(emotionalWM)
# Is this data tidy? (No. Why not?)
## Wide to long ----
# To make this data tidy, we need to make it LONG (it is now WIDE).
# We can do this with `pivot_longer()`:
emotionalWM_long <- emotionalWM |>
pivot_longer(
cols = positive_average:negative_O2,
names_to = "condition",
values_to = "mV"
)
head(emotionalWM_long)
# Is this data tidy?
## Long to wide ----
emotionalWM_long <- emotionalWM |>
pivot_longer(
cols = positive_average:negative_O2,
names_sep = "_",
names_to = "emotion", "area",
values_to = "mV"
)
head(emotionalWM_long)
# Is THIS data tidy?
# Sometimes we might want to take long form data and make it wide.
# We can do this with `pivot_wider()`
emotionalWM_wide_again <- emotionalWM_long |>
pivot_wider(
names_from = c("emotion", "area"),
values_from = "mV"
)
head(emotionalWM_wide_again)