library("broadcast")
# check for each element in one recursive array if values are present in another:
mylist <- list(
as.raw(0:255),
sample(c(TRUE, FALSE, NA), 100, TRUE),
0:255,
rnorm(10),
rnorm(10) + rnorm(10) * -1i,
sample(month.abb)
)
mylist <- c(mylist, list(mylist))
x <- array(sample(mylist, 50, TRUE), c(5, 5, 2))
y <- array(sample(mylist, 50, TRUE), c(5, 5, 2))
bcapply(x, y, `%in%`) # returns a dimensional list / recursive array
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] logical,256 logical,256 logical,10 logical,100 logical,10
## [2,] logical,10 logical,256 logical,10 logical,10 logical,10
## [3,] logical,100 logical,256 logical,6 logical,100 logical,6
## [4,] logical,256 logical,256 logical,256 logical,10 logical,256
## [5,] logical,10 logical,12 logical,10 logical,12 logical,256
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] logical,100 logical,12 logical,6 logical,12 logical,256
## [2,] logical,12 logical,12 logical,100 logical,256 logical,10
## [3,] logical,6 logical,256 logical,100 logical,256 logical,10
## [4,] logical,256 logical,100 logical,10 logical,6 logical,100
## [5,] logical,10 logical,10 logical,6 logical,256 logical,10
bcapply(x, y, \(x, y) any(x %in% y), v = "logical") # returns logical array
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] FALSE TRUE FALSE FALSE FALSE
## [2,] FALSE FALSE FALSE FALSE FALSE
## [3,] TRUE FALSE FALSE TRUE TRUE
## [4,] TRUE FALSE FALSE FALSE FALSE
## [5,] FALSE TRUE FALSE FALSE TRUE
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] FALSE TRUE FALSE TRUE FALSE
## [2,] FALSE FALSE TRUE TRUE FALSE
## [3,] FALSE FALSE FALSE TRUE FALSE
## [4,] FALSE TRUE FALSE FALSE FALSE
## [5,] FALSE FALSE FALSE FALSE FALSE
bcapply(x, y, \(x, y) all(x %in% y), v = "logical") # returns logical array
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] FALSE FALSE FALSE FALSE FALSE
## [2,] FALSE FALSE FALSE FALSE FALSE
## [3,] FALSE FALSE FALSE TRUE TRUE
## [4,] FALSE FALSE FALSE FALSE FALSE
## [5,] FALSE TRUE FALSE FALSE TRUE
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] FALSE TRUE FALSE TRUE FALSE
## [2,] FALSE FALSE FALSE FALSE FALSE
## [3,] FALSE FALSE FALSE FALSE FALSE
## [4,] FALSE FALSE FALSE FALSE FALSE
## [5,] FALSE FALSE FALSE FALSE FALSE
# calculate quartiles for each list item, and return numeric array of quartiles:
x <- list(
a = 1:10,
beta = exp(-3:3),
logic = c(TRUE,FALSE,FALSE,TRUE)
)
print(x)
## $a
## [1] 1 2 3 4 5 6 7 8 9 10
##
## $beta
## [1] 0.04978707 0.13533528 0.36787944 1.00000000 2.71828183 7.38905610
## [7] 20.08553692
##
## $logic
## [1] TRUE FALSE FALSE TRUE
quantiles <- array(c(1:3/4), c(1, 3))
colnames(quantiles) <- paste0("q = ", quantiles)
print(quantiles)
## q = 0.25 q = 0.5 q = 0.75
## [1,] 0.25 0.5 0.75
out <- bcapply(x, quantiles, \(x, y) quantile(x, probs = y), v = "double")
print(out)
## q = 0.25 q = 0.5 q = 0.75
## a 3.2500000 5.5 7.750000
## beta 0.2516074 1.0 5.053669
## logic 0.0000000 0.5 1.000000
typeof(out)
## [1] "double"bcapply
Apply Function to Pair of Arrays with Broadcasting
Description
The bcapply() method applies a function to 2 arrays element-wise with broadcasting.
Usage
bcapply(x, y, f, ...)
## S4 method for signature 'ANY'
bcapply(x, y, f, v = NULL)
Arguments
x, y
|
conformable atomic or recursive vectors/arrays. |
f
|
a function that takes in exactly 2 arguments, and returns a result that can be stored in a single element of a recursive or atomic array. |
โฆ
|
further arguments passed to or from methods. |
v
|
either NULL, or single string, giving the scalar type for a single iteration. If NULL (default) or โlistโ, the result will be a recursive array. If it is certain that, for every iteration, f() always results in a single atomic scalar of exactly a specific type, the user can specify the type in v to pre-allocate the result. Pre-allocating the results leads to slightly faster and more memory efficient code. NOTE: Incorrectly specifying v leads to undefined behaviour; when unsure, leave v at its default value. |
Value
An atomic or recursive array with dimensions bc_dim(x, y).
Preserves some of the attributes of x and y similar to broadcasted infix operators, as explained in broadcast_operators.