Index a table by a MultiFactor and apply arbitrary code to it
weave_apply-methods.Rdweave_apply can run arbitrary code specified by FUN across any number
of row-based subsets of table .data. Mimics lapply.
Arguments
- .x
a
MultiFactorobject.- .path
either a
formulaor a `character vector“ of length 2 with the names of the desired combination of feature types.- .data
A table. Adata.frame,matrix, other object with rows and columns.- .fun
A function, passed to lapply.
- ...
Additional arguments passed to lapply call.
- .index
Character scalarWhere in.xcan the target feature names be found. (Default: "row.names")
Examples
# Prepare data
x <- trade_posts()
# Generate small example feature table 'df'.
n <- nlevels(x)[["clothing"]]
df <- replicate(10, rbinom(n, rbinom(n, 100, runif(n)), runif(n)))
#' # Ensure rownames correspond to the second (RHS) variable in the formula.
df <- as.data.frame(df, row.names = levels(x)$clothing)
# Apply arbitrary code to x based on group membership
weave_apply(
x,
.path = fruit ~ clothing,
.data = df,
.fun = function(x) colSums(x)
)
#> $apples
#> V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
#> 133 81 40 64 98 36 150 83 51 101
#>
#> $pears
#> V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
#> 174 117 52 90 43 32 100 39 42 139
#>
#> $cherries
#> V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
#> 174 117 52 90 43 32 100 39 42 139
#>
#> $melons
#> V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
#> 92 81 39 57 30 28 87 39 29 85
#>
#> $grapes
#> V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
#> 215 117 53 97 111 40 163 83 64 155
#>