R/list_reduction.R
list_reduction.Rd
Successively applies a function (intersect
, by default) to elements
at each index level in a list
.
list_reduction( inlist, FUN = intersect, flatten = FALSE, sorted = FALSE, MoreArgs = NULL )
inlist | The input |
---|---|
FUN | The function to be applied. Note that the supplied function should be one
that you would expect to have work with two or more |
flatten | Logical. Should the output be simplified from a |
sorted | Logical. Should the values at each |
MoreArgs | A |
A list
(default) or a simple vector
(if flatten = TRUE
).
See: https://stackoverflow.com/q/62454705/1270695
Ananda Mahto
L <- list(colA = list(c("a", "b", "c", "ñ"), c("f", "g", "h"), c("i", "j", "k")), colB = list(c("d", "b", "e"), c("f", "g", "m", "p"), c("f", "o", "j")), colC = list(c("a", "b", "g"), c("l", "g", "f", "k", "h"), c("j", "o", "l"))) list_reduction(L)#> [[1]] #> [1] "b" #> #> [[2]] #> [1] "f" "g" #> #> [[3]] #> [1] "j" #>list_reduction(L, flatten = TRUE)#> [1] "b" "f, g" "j"set.seed(1) L2 <- replicate(3, replicate(3, sample(sample(20), sample(10), TRUE), FALSE), FALSE) list_reduction(L2)#> [[1]] #> integer(0) #> #> [[2]] #> integer(0) #> #> [[3]] #> [1] 4 5 #>list_reduction(L2, sum, flatten = TRUE)#> [1] 192 184 259list_reduction(L2, range)#> [[1]] #> [1] 1 19 #> #> [[2]] #> [1] 1 20 #> #> [[3]] #> [1] 1 19 #>list_reduction(L2, union)#> [[1]] #> [1] 5 6 4 8 1 2 9 14 7 19 3 12 18 10 17 #> #> [[2]] #> [1] 17 4 16 12 7 3 20 1 5 6 10 2 13 14 19 9 #> #> [[3]] #> [1] 3 1 2 15 12 6 8 4 14 5 9 19 10 17 13 18 7 #>list_reduction(L2, union, sorted = TRUE)#> [[1]] #> [1] 1 2 3 4 5 6 7 8 9 10 12 14 17 18 19 #> #> [[2]] #> [1] 1 2 3 4 5 6 7 9 10 12 13 14 16 17 19 20 #> #> [[3]] #> [1] 1 2 3 4 5 6 7 8 9 10 12 13 14 15 17 18 19 #>