Free Access Week - Data Engineering + BI
Data Engineering and BI courses are free this week!
Free Access Week - Jun 2-8

CHNOSZ (version 0.9-1)

util.array: Functions to Work with Multidimensional Arrays

Description

These functions can be used to turn a list into an array and extract or replace values or take the sum along a certain dimension of an array.

Usage

list2array(l)
  slice(arr, d = NULL, i = 1, value = NULL)
  dimSums(arr, d = 1, i = NULL)

Arguments

l
a list.
arr
an array.
d
numeric, what dimension to use.
i
numeric, what slice to use.
value
values to assign to the portion of an array specified by d and i.

Value

  • An array.

Details

list2array turns a list of arrays, each with the same dimensions, into a new array having one more dimension whose size is equal to the number of initial arrays.

slice extracts or assigns values from/to the ith slice(s) in the dth dimension of an array. Values are assigned to an array if value is not NULL. This function works by building an expression containing the extraction operator ([) and was written because Rdoes not (it seems) have a function that performs this operation.

dimSums sums an array along the dth dimension using only the ith slices in that dimension. If i is NULL, all slices in that dimension are summed together. For matrices, dimSums(x,1) has the same result as colSums(x) and dimSums(x,2) has the same result as rowSums(x).

In the examples below, the stopifnot tests fail unless a and b are both created as multiples of the starting matrix x. This might have something to to with the internal representation of these matrices in R.

Examples

Run this code
# start with a matrix
  x <- matrix(1:12,ncol=3)
  # pay attention to the following... it complicates
  # writing examples that test for identity!
  identical(1*x,x)   # FALSE
  # create two matrices that are multiples of the first
  a <- 1*x
  b <- 2*a
  # these both have two dimensions of lengths 4 and 3
  dim(a)  # 4 3
  # combine them to make an array with three dimensions
  c <- list2array(list(a,b))
  # the third dimension has length 2
  dim(c)  # 4 3 2
  # the first slice of the third dimension == a
  stopifnot(identical( slice(c,3), a ))
  # the second slice of the third dimension == b
  stopifnot(identical( slice(c,3,2), b ))
  # 'slice' works just like the bracket operator
  c11 <- slice(c,1)
  c12 <- slice(c,1,2)
  c21 <- slice(c,2,1)
  c212 <- slice(c,2,1:2)
  stopifnot(identical( c11, c[1,,] ))
  stopifnot(identical( c12, c[2,,] ))
  stopifnot(identical( c21, c[,1,] ))
  stopifnot(identical( c212, c[,1:2,] ))
  # let us replace part of the array
  d <- slice(c,3,2,value=a)
  # now the second slice of the third dimension == a
  stopifnot(identical( slice(d,3,2), a ))
  # and the sum across the third dimension == b
  stopifnot(identical( dimSums(d,3), b ))
  # taking the sum removes that dimension
  dim(d)             # 4 3 2
  dim(dimSums(d,1))  # 3 2
  dim(dimSums(d,2))  # 4 2
  dim(dimSums(d,3))  # 4 3

Run the code above in your browser using DataLab