-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathAppendix 01 - advance summarising with dplyr.R
55 lines (43 loc) · 1.22 KB
/
Appendix 01 - advance summarising with dplyr.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
library(dplyr)
library(parameters) # for kurtosis & skewness
e2b_data <- read.csv("data/emotional_2back.csv") |>
mutate(
Group = ifelse(Subject <= 30, 1, 2) |> factor(),
Subject = factor(Subject),
Emotion = factor(Emotion),
SameDiff = factor(SameDiff),
Gender = factor(SameDiff)
)
glimpse(e2b_data)
# You can also summarise ACROSS several variables, with `across()`.
#
# We need to define what kind of summarise we want - these can be functions,
# names of functions, or a lambda function.
suff_i_wanna_know <- list(
# a function
mean = mean,
# name of a function
sd = "sd",
# a lambda
mean_no_na = \(x) mean(x, na.rm = TRUE), # *
# Some more functions
kurt = kurtosis,
skew = skewness
)
# [*] This \(x) mean "this is a function that takes an "x" argument. It is a
# short-hand for writing function(x).
# Use the `across()` function:
e2b_data |>
summarise(
across(.cols = c(RT, ACC),
.fns = suff_i_wanna_know)
)
# other ways to select variables.
e2b_data |>
summarise(
across(.cols = c(Subject,Group), nlevels),
across(.cols = c(RT, ACC),
.fns = suff_i_wanna_know)
)
# Read more about using `across()`:
# https://dplyr.tidyverse.org/articles/colwise.html