$anna
[1] 450 520 380
$ben
[1] 610 580
$carl
[1] 400 430 470 500
We have the reaction times (in ms) of three participants, saved in a list:
$anna
[1] 450 520 380
$ben
[1] 610 580
$carl
[1] 400 430 470 500
We want the mean of each participant: apply the same function (mean) to each element of the list.
for loop[1] 450 595 450
It works, but:
i, both for rts and for meanssapply()sapply(rts, mean): apply mean to each element of rts
sapply(X, FUN)
X: the object we go through, one element at a time
FUN: the function applied to each element
mean, length, …)for loopsapply() is a compact for loop: the element of X plays the role of i
Both take 1, then 2, then 3 and multiply each by 10. sapply() collects the results in a vector.
lapply() vs sapply()Same input (rts), same function (length: how many trials each participant has)
A function that converts milliseconds into seconds…
If we need the function only once, we can write it directly inside sapply(), without saving it:
anna ben carl
140 30 100
x is a placeholder (like i in a for loop): it becomes each element of rts, one at a time
x is rts$anna: 520 - 380 = 140x is rts$ben: 610 - 580 = 30x is rts$carl: 500 - 400 = 100*apply| function | goes through … | returns |
|---|---|---|
lapply() |
each element of a list/vector | a list |
sapply() |
each element of a list/vector | a vector or matrix, if possible |
apply() |
each row or column of a matrix/dataframe | a vector |
tapply() |
each group of a vector, defined by a factor | a vector |
vapply() |
like sapply(), but checks the type of the result |
a vector |
mapply() |
several vectors/lists at the same time | a vector or a list |
apply(): rows or columnsThe scores of 3 students in 3 tests: one row for each student, one column for each test
test1 test2 test3
1 24 28 30
2 18 22 25
3 30 29 27
apply(X, MARGIN, FUN): MARGIN = 1 for the rows, MARGIN = 2 for the columns
apply(): rows or columnsMARGIN = 1: mean of each row, one result per student (first, second, third student)
MARGIN = 2: max of each column, one result per test (first, second, third test)
tapply(): by groupReaction times of 6 trials, in two conditions:
rt condition
1 450 easy
2 520 easy
3 380 easy
4 610 hard
5 580 hard
6 640 hard
tapply(): by groupIt is the same as selecting each group by hand:
[1] 450
[1] 610
vapply(): checking the resultLike sapply(), but we declare in advance what each result must be (FUN.VALUE): here, one number
mapply(): two vectors at the same timesapply() goes through one vector, mapply() goes through two (or more) vectors together
| step | a (from x) |
b (from y) |
a + b |
|---|---|---|---|
| 1 | 1 | 10 | 11 |
| 2 | 2 | 20 | 22 |
| 3 | 3 | 30 | 33 |
x and y must have the same length
mapply() with rep()Repeat each value a different number of times
mapply(): simulating dataThree samples of 3 values, each with its own mean and standard deviation:
[,1] [,2] [,3]
[1,] 9.373546 57.97640 104.8743
[2,] 10.183643 51.64754 107.3832
[3,] 9.164371 45.89766 105.7578
m = 10, s = 1, then m = 50, s = 5, then m = 100, s = 10SIMPLIFY = FALSE to get a list instead of a matrixreplicate(): repeat the same codereplicate(n, expr): run the code expr n times. There is no element to go through: the code is just repeated.
[1] 102.47375 103.98324 103.30834 103.39998 90.09916
Each number is the mean of a new sample of 30 values: the means are all different, but close to 100.
replicate(): sampling distributionRepeating it 1000 times we get the sampling distribution of the mean:
Open this link and keep it open: