Title: | Subset- And Name-Aware Array Utility Functions |
---|---|
Description: | Stacking arrays according to dimension names, subset-aware splitting and mapping of functions, intersecting along arbitrary dimensions, converting to and from data.frames, and many other helper functions. |
Authors: | Michael Schubert <[email protected]> |
Maintainer: | Michael Schubert <[email protected]> |
License: | Apache License (== 2.0) | file LICENSE |
Version: | 0.5.1 |
Built: | 2025-01-20 09:22:19 UTC |
Source: | https://github.com/mschubert/narray |
Operator for array-like logical operations
a %or% b
a %or% b
a |
First vector |
b |
Second vector |
TRUE/FALSE for each element
Binds arrays together disregarding names
bind(..., along = length(dim(arrayList[[1]])) + 1)
bind(..., along = length(dim(arrayList[[1]])) + 1)
... |
N-dimensional arrays, or a list thereof |
along |
Along which axis to bind them together (default: new axis) |
A joined array
This currently only supports x with only one non-zero element
collect(x, along = 2)
collect(x, along = 2)
x |
A logical matrix |
along |
Which axis to spread mask on |
A character vector or list thereof
The construct() function can be called either with the data.frame as the first argument or the formula and then specify 'data=<data.frame>'
construct(data, formula = guess_structure(data), fill = NA, name_axes = TRUE)
construct(data, formula = guess_structure(data), fill = NA, name_axes = TRUE)
data |
A data frame |
formula |
A formula: value ~ axis1 [+ axis2 + axis n ..] |
fill |
Value to fill array with if undefined |
name_axes |
Keep column names of 'data' as axis names |
A structured array
base::dim, but returning 1 for vector
dim(x)
dim(x)
x |
Object to get dimensions on |
Act on each element if 'x' is a list
dimnames(x, along = TRUE, null_as_integer = FALSE, drop = !identical(along, TRUE))
dimnames(x, along = TRUE, null_as_integer = FALSE, drop = !identical(along, TRUE))
x |
An n-dimensional array |
along |
Limit to dimension (default: all) |
null_as_integer |
Whether nameless dimensions should be |
drop |
Drop list of only one axis requested (default: if not returning all dimensions) |
A list of dimension names with length length(ndim(X))
Drop unused dims if flag is TRUE
drop_if(x, flag)
drop_if(x, flag)
x |
An array object |
flag |
Whether to drop unused dimensions |
The object in full or with dropped dimensions
Function to discard subsets of an array (NA or drop)
filter(X, along, FUN, subsets = base::rep(1, dim(X)[along]), na.rm = FALSE)
filter(X, along, FUN, subsets = base::rep(1, dim(X)[along]), na.rm = FALSE)
X |
An n-dimensional array |
along |
Along which axis to apply |
FUN |
Function to apply, needs to return |
subsets |
Subsets that should be used when applying |
na.rm |
Whether to omit columns and rows with |
An array where filtered values are NA
or dropped
Flattens an array along an axis
flatten(x, along = -1, name_sep = NA)
flatten(x, along = -1, name_sep = NA)
x |
Array |
along |
Along which axis to bind them together (default: last) |
name_sep |
Which character to use for naming new arrays [default: NA, do not touch names] |
An array with n-1 dimensions
Infer array structure from data.frame
guess_structure(df, verbose = TRUE)
guess_structure(df, verbose = TRUE)
df |
A data.frame with ordered axes, value field last |
verbose |
Print message with inferred structure (default: TRUE) |
A formula describing this structure
TODO: accept along=c(1,2,1,1...) [maybe list w/ vectors as well?] TODO: accept data=env/list arg? [sig-comb/drug-tissue/assocs.r#62-65]
intersect(..., along = 1, envir = parent.frame(), drop = FALSE, fail_if_empty = TRUE)
intersect(..., along = 1, envir = parent.frame(), drop = FALSE, fail_if_empty = TRUE)
... |
Arrays that should be intersected |
along |
The axis along which to intersect |
envir |
A list or environment to act upon |
drop |
Drop unused dimensions on result |
fail_if_empty |
Stop if intersection yields empty set |
Intersects a lits of arrays for common dimension names
intersect_list(l., along = 1, drop = FALSE, fail_if_empty = TRUE)
intersect_list(l., along = 1, drop = FALSE, fail_if_empty = TRUE)
l. |
List of arrays to perform operations on |
along |
The axis along which to intersect |
drop |
Drop unused dimensions on result |
fail_if_empty |
Stop if intersection yields empty set |
Lambda syntax for array iteration
lambda(fml, along, group = c(), simplify = TRUE, expand_grid = TRUE, envir = parent.frame())
lambda(fml, along, group = c(), simplify = TRUE, expand_grid = TRUE, envir = parent.frame())
fml |
A call prefixed with a tilde |
along |
A named vector which objects to subset (eg: c(x=1)) |
group |
Not implemented |
simplify |
Return array instead of index+result if scalar |
expand_grid |
Use all combinations of indices (default: TRUE) |
envir |
Environment where variables can be found |
x
to be like like
, including dimension namesReshapes x
to be like like
, including dimension names
like(x, like)
like(x, like)
x |
An n-dimensional array |
like |
An n-dimensional array whose form |
An array with values of X
and structure of like
Maps a function along an array preserving its structure
map(X, along, FUN, subsets = base::rep(1, dim(X)[along]), drop = TRUE, ...)
map(X, along, FUN, subsets = base::rep(1, dim(X)[along]), drop = TRUE, ...)
X |
An n-dimensional array |
along |
Along which axis to apply the function |
FUN |
A function that maps a vector to the same length or a scalar |
subsets |
Whether to apply |
drop |
Remove unused dimensions after mapping; default: TRUE |
... |
Other arguments passed to |
An array where FUN
has been applied
Apply function that preserves order of dimensions
map_one(X, along, FUN, pb, drop = TRUE, ...)
map_one(X, along, FUN, pb, drop = TRUE, ...)
X |
An n-dimensional array |
along |
Along which axis to apply the function |
FUN |
A function that maps a vector to the same length or a scalar |
pb |
progress bar object |
drop |
Remove unused dimensions after mapping; default: TRUE |
... |
Arguments passed to the function |
An array where FUN
has been applied
Converts a list of character vectors to a logical matrix
mask(x, along = 2, na_rm = FALSE)
mask(x, along = 2, na_rm = FALSE)
x |
A list of character vectors |
along |
Which axis to spread mask on |
na_rm |
Remove values that were translated to NAs |
A logical occurrence matrix
match() function with extended functionality
match(x, from, to, filter_from = NULL, filter_to = NULL, data = parent.frame(), fuzzy_level = 0, table = FALSE, na_rm = FALSE, warn = !table && fuzzy_level > 0)
match(x, from, to, filter_from = NULL, filter_to = NULL, data = parent.frame(), fuzzy_level = 0, table = FALSE, na_rm = FALSE, warn = !table && fuzzy_level > 0)
x |
Vector of identifiers that should be mapped |
from |
Vector of identifiers that can be mapped |
to |
Matched mapping for all identifiers |
filter_from |
Restrict matching to a subset from 'from' |
filter_to |
Restrict matching to a subset from 'to' |
data |
List containing the data 'from' and 'to' reference |
fuzzy_level |
0 for exact, 1 punctuation, and 2 closest character |
table |
Return a matching table instead of just the matches |
na_rm |
Flag to remove items that can not be mapped |
warn |
Display warning for all fuzzy matches |
Mapped values
Function to melt data.frame from one or multiple arrays
melt(..., dimnames = NULL, na_rm = TRUE)
melt(..., dimnames = NULL, na_rm = TRUE)
... |
Array[s] or data.frame[s] to be melted |
dimnames |
List of names along the dimensions |
na_rm |
Remove rows with NAs |
data.frame with 'value' (or object names if multiple) indexed by axes
Return a list of named dot-arguments
named_dots(...)
named_dots(...)
... |
Function arguments |
Named function arguments
Stacking arrays according to dimension names, subset-aware splitting and mapping of functions, intersecting along arbitrary dimensions, converting to and from data.frames, and many other helper functions.
Progress bar format to be consistent
pb(ticks)
pb(ticks)
ticks |
Number of ticks the bar has |
A progress bar object
Repeats an array along an arbitrary axis
rep(x, n, along = 1) crep(x, n) rrep(x, n)
rep(x, n, along = 1) crep(x, n) rrep(x, n)
x |
An array object |
n |
Integer, how often to repeat |
along |
Along which axis to repeat (default: 1) |
An array that is repeated 'n' times on axis 'along'
If no dimnames, return NULL and not list of NULLs
restore_null_dimnames(x)
restore_null_dimnames(x)
x |
An array object |
The object with NULL if no dimnames
Splits and array along a given axis, either totally or only subsets
split(X, along, subsets = c(1:dim(X)[along]), drop = NULL)
split(X, along, subsets = c(1:dim(X)[along]), drop = NULL)
X |
An array that should be split |
along |
Along which axis to split; use -1 for highest dimension |
subsets |
Whether to split each element or keep some together |
drop |
Remove unused dimensions after mapping default: drop if all resulting arrays have same number of dimensions |
A list of arrays that combined make up the input array
Stacks arrays while respecting names in each dimension
stack(..., along = length(dim(arrayList[[1]])) + 1, fill = NA, drop = FALSE, keep_empty = FALSE, allow_overwrite = FALSE, fail_if_empty = TRUE)
stack(..., along = length(dim(arrayList[[1]])) + 1, fill = NA, drop = FALSE, keep_empty = FALSE, allow_overwrite = FALSE, fail_if_empty = TRUE)
... |
N-dimensional arrays, or a list thereof |
along |
Which axis arrays should be stacked on (default: new axis) |
fill |
Value for unknown values (default: |
drop |
Drop unused dimensions (default: FALSE) |
keep_empty |
Keep empty elements when stacking (default: FALSE) |
allow_overwrite |
Overwrite values if more arrays share same key |
fail_if_empty |
Stop if no arrays left after removing empty elements |
A stacked array, either n or n+1 dimensional
Subsets an array using a list with indices or names
subset(X, index, along = -1, drop = FALSE)
subset(X, index, along = -1, drop = FALSE)
X |
The array to subset |
index |
A list of vectors to use for subsetting, or vector if along is given |
along |
Along which dimension to subset if index is a vector; default is last dimension; argument is ignored if X is a vector |
drop |
Remove unused dimensions after mapping; default: TRUE |
The subset of the array
Translate an axis between two sets of identifiers
translate(x, along = 1, to, from = dimnames(x)[[along]], ..., FUN, na_rm = FALSE)
translate(x, along = 1, to, from = dimnames(x)[[along]], ..., FUN, na_rm = FALSE)
x |
A matrix |
along |
Along which axis to summarize |
to |
Names that this dimension should be summarized to |
from |
Names that match the dimension 'along' |
... |
Parameters passed to 'match' |
FUN |
Which function to apply, default is throwing error on aggregation |
na_rm |
Remove values that were translated to NAs |
A summarized matrix as defined by 'from', 'to'
Converts vectors in a list to row- or column vectors
vectors_to_row_or_col(xlist, along)
vectors_to_row_or_col(xlist, along)
xlist |
List of array-like elements and vectors |
along |
Along which dimension vectors should be aligned |
List where vectors are replaced by row- or col vectors (2d)
which
functionA multidimensional which
function
which(x, drop = TRUE)
which(x, drop = TRUE)
x |
N-dimensional logical array |
drop |
Return a vector if called on a vector |
A matrix with indices where A == TRUE