Title: | Convenience Functions for Arrays |
---|---|
Description: | Some convenient functions to work with arrays. |
Authors: | C. Beleites <[email protected]> |
Maintainer: | C. Beleites <[email protected]> |
License: | GPL |
Version: | 1.1-0 |
Built: | 2024-11-28 02:52:17 UTC |
Source: | https://github.com/r-forge/arrayhelpers |
Little helper functions to work with arrays
C. Beleites
Maintainer: Claudia Beleites <[email protected]>
array2df: Convert multidimensional array into matrix or data.frame
The "wide-format" array is converted into a "long-format" matrix
or
data.frame
.
array2df( x, levels, matrix = FALSE, label.x = deparse(substitute(x)), na.rm = FALSE )
array2df( x, levels, matrix = FALSE, label.x = deparse(substitute(x)), na.rm = FALSE )
x |
|
levels |
If If If
|
matrix |
If |
label.x |
Name for the column containing the |
na.rm |
should rows where the value of |
If the resulting data.frame
is too large to fit in memory, a
matrix
might help.
The main benefit of this function is that it uses matrices as long as possible. This can give large advantages in terms of memory consumption.
A data.frame or matrix with prod (dim (x))
rows and length (dim (x)) + 1
columns.
Claudia Beleites
a <- arrayhelpers:::a a array2df (a) array2df (a, matrix = TRUE) array2df (a, levels = list(NULL, x = NA, c = NULL), label.x = "value") array2df (a, levels = list(NULL, x = TRUE, c = c ("foo", "bar")), label.x = "value") summary (array2df (a, levels = list(NULL, x = NA, c = c ("foo", "bar")), label.x = "value")) summary (array2df (a, levels = list(NULL, x = NA, c = c ("foo", "bar")), label.x = "value", matrix = TRUE))
a <- arrayhelpers:::a a array2df (a) array2df (a, matrix = TRUE) array2df (a, levels = list(NULL, x = NA, c = NULL), label.x = "value") array2df (a, levels = list(NULL, x = TRUE, c = c ("foo", "bar")), label.x = "value") summary (array2df (a, levels = list(NULL, x = NA, c = c ("foo", "bar")), label.x = "value")) summary (array2df (a, levels = list(NULL, x = NA, c = c ("foo", "bar")), label.x = "value", matrix = TRUE))
array
s are numeric
s with a dim
attribute and are
stored with the first index moving fastest (i.e. by column). They can be
indexed both ways.
array2vec(iarr, dim) vec2array(ivec, dim)
array2vec(iarr, dim) vec2array(ivec, dim)
iarr |
vector with the indices into the array dimensions |
dim |
vector with the array dimensions, as returned by |
ivec |
scalar with the index into the vector |
array2vec
returns a scalar, vec2array
a
matrix
.
C. Beleites
see Extract
on the difference of indexing an
array
with a vector or a matrix
.
arr <- array (rnorm (24), dim = 2 : 4) arr v <- matrix(c(2, 2, 2), nrow = 1) i <- array2vec (v, dim = dim (arr)) i arr[v] arr[i] arr[c(2, 2, 2)] ## indexing with a vector arr[2] i <- 14 v <- vec2array (i, dim = dim (arr)) v arr [v] arr [i]
arr <- array (rnorm (24), dim = 2 : 4) arr v <- matrix(c(2, 2, 2), nrow = 1) i <- array2vec (v, dim = dim (arr)) i arr[v] arr[i] arr[c(2, 2, 2)] ## indexing with a vector arr[2] i <- 14 v <- vec2array (i, dim = dim (arr)) v arr [v] arr [i]
Run the unit tests attached to the functions via svUnit
arrayhelpers.unittest()
arrayhelpers.unittest()
invisibly TRUE
if the tests pass, NA
if svUnit is not
available. Stops if errors are encountered.
Claudia Beleites
These functions extend the respective base functions by (optionally) preserving the shape of the array (i.e. the summed dimensions have length 1).
## S4 method for signature 'matrix' colSums(x, na.rm = FALSE, dims = 1L, drop = TRUE) colSums.AsIs(x, ...) ## S4 method for signature 'array' colSums(x, na.rm = FALSE, dims = 1L, drop = TRUE) ## S4 method for signature 'matrix' colMeans(x, na.rm = FALSE, dims = 1L, drop = TRUE) colMeans.AsIs(x, ...) ## S4 method for signature 'array' colMeans(x, na.rm = FALSE, dims = 1L, drop = TRUE) ## S4 method for signature 'matrix' rowSums(x, na.rm = FALSE, dims = 1L, drop = TRUE) rowSums.AsIs(x, ...) ## S4 method for signature 'array' rowSums(x, na.rm = FALSE, dims = 1L, drop = TRUE) ## S4 method for signature 'matrix' rowMeans(x, na.rm = FALSE, dims = 1L, drop = TRUE) rowMeans.AsIs(x, ...) ## S4 method for signature 'array' rowMeans(x, na.rm = FALSE, dims = 1L, drop = TRUE)
## S4 method for signature 'matrix' colSums(x, na.rm = FALSE, dims = 1L, drop = TRUE) colSums.AsIs(x, ...) ## S4 method for signature 'array' colSums(x, na.rm = FALSE, dims = 1L, drop = TRUE) ## S4 method for signature 'matrix' colMeans(x, na.rm = FALSE, dims = 1L, drop = TRUE) colMeans.AsIs(x, ...) ## S4 method for signature 'array' colMeans(x, na.rm = FALSE, dims = 1L, drop = TRUE) ## S4 method for signature 'matrix' rowSums(x, na.rm = FALSE, dims = 1L, drop = TRUE) rowSums.AsIs(x, ...) ## S4 method for signature 'array' rowSums(x, na.rm = FALSE, dims = 1L, drop = TRUE) ## S4 method for signature 'matrix' rowMeans(x, na.rm = FALSE, dims = 1L, drop = TRUE) rowMeans.AsIs(x, ...) ## S4 method for signature 'array' rowMeans(x, na.rm = FALSE, dims = 1L, drop = TRUE)
x |
an array of two or more dimensions, containing numeric, complex, integer or logical values, or a numeric data frame. |
na.rm |
logical indicating treatment of missing values |
dims |
integer: Which dimensions are regarded as ‘rows’ or ‘columns’ to sum
over. For |
drop |
If |
... |
the |
like colSums
if drop = TRUE
, otherwise an array where the
summed dimensions have length 1.
Claudia Beleites
a <- array (1 : 24, 4 : 2) a rowSums (a) rowSums (a, drop = FALSE) colSums (a) colSums (a, drop = FALSE) colSums (a, dim = 2) colSums (a, dim = 2, drop = FALSE)
a <- array (1 : 24, 4 : 2) a rowSums (a) rowSums (a, drop = FALSE) colSums (a) colSums (a, drop = FALSE) colSums (a, dim = 2) colSums (a, dim = 2, drop = FALSE)
matrices are converted to data.frame.
countRows(x)
countRows(x)
x |
the matrix or data.frame |
data frame with unique rows, their counts and indices into the original data.frame
this function is subject to changes in the future.
Claudia Beleites
Convenient for printing
delold(a)
delold(a)
a |
the array |
a
stripped of the old.*
attributes.
Claudia Beleites
a <- arrayhelpers:::a makeNd (a, 2) delold (makeNd (a, 2))
a <- arrayhelpers:::a makeNd (a, 2) delold (makeNd (a, 2))
NULL
Drop dimnames if all elements are NULL
dropdimnames(x) lon(l)
dropdimnames(x) lon(l)
x |
object |
l |
list |
object without empty dimnames
lon
: NULL
if all elements of dn
are NULL
, otherwise dn
Claudia Beleites
ensuredim
turns vectors into 1d-arrays, and leaves arrays unchanged. drop1d
is the
inverse: it converts 1d arrays into vectors.
ensuredim(x) drop1d(x, drop = TRUE)
ensuredim(x) drop1d(x, drop = TRUE)
x |
vector (or array) |
drop |
if |
esuredim
array of at least one dimension
drop1d
vector, if x
had only 1 dimension
Claudia Beleites
Claudia Beleites
rowsum
groupsum
extends rowsum
: it allows group
to be an array of the same shape
as x
.
groupsum( x, group = NULL, dim = 1L, reorder = TRUE, na.rm = FALSE, ..., drop = !is.array(x) )
groupsum( x, group = NULL, dim = 1L, reorder = TRUE, na.rm = FALSE, ..., drop = !is.array(x) )
x |
array to be |
group |
grouping variable (integer or factor) indicating groups of samples. |
dim |
along which dimension should the group sums be taken? (default: rows) |
reorder |
should the groups be ordered? see |
na.rm |
shoud |
... |
ignored |
drop |
should 1d arrays drop to vectors? |
like rowsum
, but further dimensions of the array are preserved.
Claudia Beleites
n
dimensions and restore the old dimensionsnameNd
ensures a given number of dimensions:
If a
has less than N
dimensions, new dimensions of length 1 are appended.
If a
has more than N
dimensions, the supernumerary dimensions are collapsed onto
the last dimension.
Attributes old.dim
and old.dimnames
are used by default. restoredim
is the
inverse of makeNd
.
makeNd(a, N) restoredim( a, old = NULL, n = 1L, ..., usedim = TRUE, fromend = FALSE, drop = FALSE )
makeNd(a, N) restoredim( a, old = NULL, n = 1L, ..., usedim = TRUE, fromend = FALSE, drop = FALSE )
a |
an array (matrix, vector) |
N |
the desired number of dimensions, 0 to remove the |
old |
list containing a list with (possibly) elements |
n |
how many makeNdim steps to go back? |
... |
ignored |
usedim |
use only the specified dimensions |
fromend |
if |
drop |
should 1d arrays drop to vectors? |
Note that missing attributes as well as old.dim = NULL
produce a (dimensionless)
vector. This is also the case if a
lost the old.*
attributes during
computations like as.numeric
, c
, etc..
fromend
together with numeric usedim
specifies dimensions counting from the
end. E.g. fromend = TRUE
and usedim = 1 : 3
for an array to be restored to 10d
means restoring dimensions 8 : 10. fromend = TRUE
and usedim = -(1 : 3)
restores
dimensions 1 to 7.
N-dimensional array
an array
Claudia Beleites
Claudia Beleites
v <- arrayhelpers:::v v makeNd (v, 1) dim (makeNd (v, 1)) dim (makeNd (v, 3)) m <- arrayhelpers:::m m makeNd (m, 1) dim (makeNd (m, 1)) makeNd (m, 0) dim (makeNd (m, 0)) makeNd (m, 3) a <- arrayhelpers:::a a dim (makeNd (a, 1)) dim (makeNd (a, 0)) makeNd (a, 2) makeNd (a, -2) makeNd (a, -4) makeNd (a, 3); a <- array (1 : 24, 4 : 3) a restoredim (makeNd (a, 0)) x <- makeNd (a, 0) attr (x, "old")
v <- arrayhelpers:::v v makeNd (v, 1) dim (makeNd (v, 1)) dim (makeNd (v, 3)) m <- arrayhelpers:::m m makeNd (m, 1) dim (makeNd (m, 1)) makeNd (m, 0) dim (makeNd (m, 0)) makeNd (m, 3) a <- arrayhelpers:::a a dim (makeNd (a, 1)) dim (makeNd (a, 0)) makeNd (a, 2) makeNd (a, -2) makeNd (a, -4) makeNd (a, 3); a <- array (1 : 24, 4 : 3) a restoredim (makeNd (a, 0)) x <- makeNd (a, 0) attr (x, "old")
number of dimensions
ndim(a)
ndim(a)
a |
vector, matrix, or array |
integer: length of dim attribute
Claudia Beleites
Convert character or logical indices to numeric
numericindex(x, i, n = names(x))
numericindex(x, i, n = names(x))
x |
the object that is to be indexed |
i |
the indices to be converted |
n |
names of the object |
numeric indices
Claudia Beleites
TODO: implement as reference class?
Note: pop
only removes elements. To retrieve them, use peek
.
peek(x, an, n = 1L) pop(x, an, n = 1L) push (x, an) <- value
peek(x, an, n = 1L) pop(x, an, n = 1L) push (x, an) <- value
x |
the object |
an |
attribute holding the stack |
n |
numer of element to peek at and numer of elements to pop (delete), respectively |
value |
list of things to push on the stack. |
push
and pop
: the object with stack in list an
pushed/popped by the n
elements
peek
: the n
th stack element (without popping!)
Claudia Beleites
This function extends the base function rowsum
.
## S4 method for signature 'array' rowsum(x, group, reorder = TRUE, na.rm = FALSE, ...)
## S4 method for signature 'array' rowsum(x, group, reorder = TRUE, na.rm = FALSE, ...)
x |
array to be |
group |
grouping variable (integer or factor) indicating groups of samples. |
reorder |
should the groups be ordered? see |
na.rm |
shoud |
... |
ignored |
like rowsum
, but further dimensions of the array are preserved.
Claudia Beleites
slice
is an alternative interface to [
(extract).
Dimensions to index must be given by name, i for the first, j for the second and so on.
slice(a, ..., drop = TRUE) slice (a, ...) <- value
slice(a, ..., drop = TRUE) slice (a, ...) <- value
a |
vector, matrix, or array |
... |
indexing instructions. The names of the arguments specify the dimension
(i = 1st, j = 2nd, ...). The indexing expressions are the same as for |
drop |
see |
value |
the values to assign |
array
Claudia Beleites
slice (arrayhelpers:::a, j = 3 : 2) tmp <- arrayhelpers:::a slice (tmp, j = 2 : 3) <- 0 tmp
slice (arrayhelpers:::a, j = 3 : 2) tmp <- arrayhelpers:::a slice (tmp, j = 2 : 3) <- 0 tmp
This function provides transposing of arrays or vectors as swapping their first two dimensions.
t (array)
can be enabled via setMethod
, see the example.
ta(x)
ta(x)
x |
an array |
the array with the first two dimensions swapped.
Claudia Beleites
a <- array (1 : 24, 4:2) a ta (a) setMethod ("t", "array", ta) t (a) removeMethod ("t", "array")
a <- array (1 : 24, 4:2) a ta (a) setMethod ("t", "array", ta) t (a) removeMethod ("t", "array")