For each element of a list, apply function then combine results into an array.
Usage
laply(
.data,
.fun = NULL,
...,
.progress = "none",
.inform = FALSE,
.drop = TRUE,
.parallel = FALSE,
.paropts = NULL
)Arguments
- .data
list to be processed
- .fun
function to apply to each piece
- ...
other arguments passed on to
.fun- .progress
name of the progress bar to use, see
create_progress_bar- .inform
produce informative error messages? This is turned off by default because it substantially slows processing speed, but is very useful for debugging
- .drop
should extra dimensions of length 1 in the output be dropped, simplifying the output. Defaults to
TRUE- .parallel
if
TRUE, apply function in parallel, using parallel backend provided by foreach- .paropts
a list of additional options passed into the
foreachfunction when parallel computation is enabled. This is important if (for example) your code relies on external data or packages: use the.exportand.packagesarguments to supply them so that all cluster nodes have the correct environment set up for computing.
Value
if results are atomic with same type and dimensionality, a vector, matrix or array; otherwise, a list-array (a list with dimensions)
Details
laply is similar in spirit to sapply except
that it will always return an array, and the output is transposed with
respect sapply - each element of the list corresponds to a row,
not a column.
Output
If there are no results, then this function will return a vector of
length 0 (vector()).
References
Hadley Wickham (2011). The Split-Apply-Combine Strategy for Data Analysis. Journal of Statistical Software, 40(1), 1-29. https://www.jstatsoft.org/v40/i01/.
Examples
laply(baseball, is.factor)
#> [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
#> [13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
# cf
ldply(baseball, is.factor)
#> .id V1
#> 1 id FALSE
#> 2 year FALSE
#> 3 stint FALSE
#> 4 team FALSE
#> 5 lg FALSE
#> 6 g FALSE
#> 7 ab FALSE
#> 8 r FALSE
#> 9 h FALSE
#> 10 X2b FALSE
#> 11 X3b FALSE
#> 12 hr FALSE
#> 13 rbi FALSE
#> 14 sb FALSE
#> 15 cs FALSE
#> 16 bb FALSE
#> 17 so FALSE
#> 18 ibb FALSE
#> 19 hbp FALSE
#> 20 sh FALSE
#> 21 sf FALSE
#> 22 gidp FALSE
colwise(is.factor)(baseball)
#> id year stint team lg g ab r h X2b X3b hr rbi
#> 1 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
#> sb cs bb so ibb hbp sh sf gidp
#> 1 FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
laply(seq_len(10), identity)
#> [1] 1 2 3 4 5 6 7 8 9 10
laply(seq_len(10), rep, times = 4)
#> 1 2 3 4
#> [1,] 1 1 1 1
#> [2,] 2 2 2 2
#> [3,] 3 3 3 3
#> [4,] 4 4 4 4
#> [5,] 5 5 5 5
#> [6,] 6 6 6 6
#> [7,] 7 7 7 7
#> [8,] 8 8 8 8
#> [9,] 9 9 9 9
#> [10,] 10 10 10 10
laply(seq_len(10), matrix, nrow = 2, ncol = 2)
#> , , 1
#>
#> 1 2
#> [1,] 1 1
#> [2,] 2 2
#> [3,] 3 3
#> [4,] 4 4
#> [5,] 5 5
#> [6,] 6 6
#> [7,] 7 7
#> [8,] 8 8
#> [9,] 9 9
#> [10,] 10 10
#>
#> , , 2
#>
#> 1 2
#> [1,] 1 1
#> [2,] 2 2
#> [3,] 3 3
#> [4,] 4 4
#> [5,] 5 5
#> [6,] 6 6
#> [7,] 7 7
#> [8,] 8 8
#> [9,] 9 9
#> [10,] 10 10
#>