Multidimensional Array
This R library is a replica of ndarray and functionality from NumPy. This functionality can also be found to some extent in MATLAB and to a lesser extent in APL.
DIM()returns the dimension of an object or its length.ndim()returns the number of dimensions of an array.nsize()returns the number of elements of an array.ndmin()ensures a minimum number of dimensions in an array.ensuredim()anddropdim()enforces an array or a vector.dimC()set the dimension of an object in row-major ordering (C-style).reshape.array()reshapes an array to new dimension.resize.array()resizes an array to new dimension.broadcastDIM()retrieves broadcast dimensions.reshape_broadcast()reshapes array on basis of broadcasting.save.array()andload.array()writes and reads arrays to/from files.marray()andas.marray()transform data into a multidimensional array.foreach.array()applies a function to the elements of an array for axes.apply_over_axes()applies a function over or along axes of an array.as.marray_int(),as.marray_dbl(),as.marray_raw(),as.marray_cpx(),as.marray_chr()andas.marray_lgl()convert the elements of an array into corresponding type.as.marray_norm()converts the elements of an array into its normal distribution.atleast_1d(),atleast_2d()andatleast_3d()coerce objects into arrays with corresponding number of dimensions.flatten()andravelflattens data into a one-dimensional array.flatten_int(),flatten_dbl(),flatten_raw(),flatten_cpx(),flatten_chr()andflatten_lgl()flatten data into a vector from corresponding type.ravel_multi_indexconverts a list of indices into an array of flat indices.expand_dims()expands the shape of an array by inserting a new axis.squeeze(),squeeze_first()andsqueeze_last()removes dimensions of length one from array.mamatrix()shrinks an array by rows or columns into a matrix.slice()read or write a part of an array.take()extracts elements from an array along an axis.extract()returns values from an array that meet conditions.axesindices()retrieves axes indices of values within an array.maclip()limits values of an array.argmax()returns indices of maximum values along an axis.argmin()returns indices of minimum values along an axis.argwhere()returns indices of elements that meet condition.amax()returns maximum values along an axis.amin()returns minimum values along an axis.madiff()calculates the n-th discrete difference along axis.count_nonzero()counts the number of non-zero values along an axis.flatnonzero()returns indices of non-zero elements within a flatten array.trim_zeros()trims leading and/or trailing zeros from 1D array.mabind()combines input arrays to an output array along a specified axis.vstack()stacks arrays in sequence vertically (row-wise).hstack()stacks arrays in sequence horizontally (column-wise).dstack()stacks arrays in sequence along 3rd axis (depth-wise).column_stack()stacks 1D arrays as columns into a 2D array.row_stack()stacks 1D arrays as rows into a 2D array.array_split()splits an array into sub-arrays along an axis.vsplit()splits an array into sub-arrays vertically (row-wise).hsplit()splits an array into sub-arrays horizontally (column-wise).dsplit()splits an array into sub-arrays along 3rd axis (depth-wise).marepeat()repeats elements of an array.tile()repeats an array a number of times.eye()creates a 2D identity matrix.vander()creates a Vandermonde matrix.ones()andones_likecreates an array filled with ones.zeros()andzeros_likecreates an array filled with zeros.empty()andempty_likecreates an array filled with NA.full()andfull_likecreates an array filled with value.random_int(),random_int_like,random_dbl(),random_dbl_like()andrandorm_normal()creates an array filled with random values.maidentity()creates an identity array.insert()inserts objects into an array.copyto()copies values from one array to another.delete()deletes parts from an array.erase()deletes axis from an array.t.array()andtranspose()transpose an array by reversing or swapping dimensions.rearrange()rearranges axis-driven an array by swapping dimensions.swapaxes()interchanges two axes of an array.moveaxis()moves axes of an array to new positions.flip()reverses the order of the elements of an array along axes.flipud()flips an array vertically (axis = 1).fliplr()flips an array horizontally (axis = 2).rot90()rotates an array by 90 degrees in the plane specified by axes.roll()shifts an array circularly.crop()takes out a part of an array with default values for the remaining part.pad()pads an array.slide()slides over an array with a window of given size and given stride.embedseries()resamples data into an ongoing shifted series array.rescale.array()rescales values in an array.place()replaces values in an array.where()creates an array based on meeting conditions.bitwise()combines arrays by bitwise operation of their elements.memberof(),mask_rectangleandmask_circlecreate binary arrays (masked arrays) consisting of zeros and ones.sparse_to_onehot()andone_to_sparse()converts an array from one encoded form into the other.bartlett()creates the Bartlett window.bblackman()creates the Blackman window.hamming()creates the Hamming window.hanning()creates the Hanning window.kaiser()creates the Kaiser window.
library(marray)
# Filled with NA
a <- empty(dim = c(4, 3, 2))
# Filled with Zeros
a <- zeros(dim = c(4, 3, 2))
# Filled with Ones
a <- ones(dim = c(4, 3, 2))
# Filled with value(s)
a <- full(dim = c(4, 3, 2), fill_value = 11)
a <- full(dim = c(4, 3, 2), fill_value = c(11, 2, 23), order = "F")# Row-major ordering
a <- marray(1:24, dim = c(4, 3, 2))
# Column-major ordering
a <- marray(1:24, dim = c(4, 3, 2), order = "F")
# Different types of data
v <- (1:24)
l <- list(x1 = 1:10, x2 = seq(10, 100, 10))
df <- data.frame(x1 = 1:6, x2 = seq(10, 60, 10), x3 = sample(letters, 6))
m <- matrix(1:24, nrow = 6)
a1 <- array(letters[1L:24L])
a3 <- array(v, dim = c(4, 3, 2))
a4 <- array(1:48, dim = c(4, 3, 2, 2))
a <- marray(v) # just change the argumenta <- marray(seq_len(12))
a <- expand_dims(a)
a <- squeeze(a)a4 <- array(1:48, dim = c(4, 3, 2, 2))
flatten(a4, order = "F")x <- marray(seq_len(24), dim = c(4, 3, 2), order = "F")
y <- marray(-seq_len(24), dim = c(4, 3, 2), order = "F")
z <- marray(seq_len(12), dim = c(4, 3))
a <- expand_dims(z) |>
list() |>
append(list(x, y), 0L) |>
mabind()a <- marray(seq.int(2 * 3 * 4), dim = c(2, 3, 4), order = "F")
x <- marray(100L + seq.int(2 * 1 * 4))
insert(a, x, axis = 2L, order = "F") # x will automatically be coerced in the right shapea <- marray(1:48, dim = c(4, 3, 2, 2))
slice(a) # read complete four-dimensional array
slice(a, l = 2) # the values of the second element of the last dimension (4th dimension)
slice(a, i = 1, j = 3) # the values of the first element of the first dimension (1st row) and the third element of the second dimension (3rd column) across all bunches of the remaining dimensions 3 and 4.
a <- marray(1:24, dim = c(4, 3, 2), order = "F")
slice(a, i = 1L) <- 0L; a # write 0 to the first dimension (row) across all remaining dimensions
slice(a, i = 1L) <- 100:102; a # write 100-102 to the first dimension (row) across all remaining dimensions
slice(a, i = 1L) <- 100:105; a # write 100-105 to the first dimension (row) across all remaining dimensions
slice(a, i = 1L) <- matrix(100:105, nrow = 3L); a # equal to prior, nrow can be 1, 2, 3, or 6a <- marray(seq_len(24), dim = c(4, 3, 2), order = "F")
# Along first dimension
flip(a)
# Along second dimension
flip(a, axis = 2L)
# Along third dimension
flip(a, axis = 3L)
# Along first and second dimension
flip(a, axis = c(1L, 2L))a <- marray(seq_len(12), dim = c(4, 3), order = "F")
# Clockwise
rot90(a)
# Two times clockwise
rot90(a, k = 2L)
# Counterclockwise
rot90(a, k = -1L)
# Three times counterclockwise
rot90(a, k = -3L)a <- marray(1:24, dim = c(6, 4), order = "F")
embedseries(a, length = 3L)a <- marray(0:9)
roll(a, shift = 2)
roll(a, shift = -2)
a <- marray(a, dim = c(2, 5))
roll(a, shift = 1)
roll(a, shift = -1)
roll(a, shift = 1, axis = 1)
roll(a, shift = -1, axis = 1)a <- marray(1:24, dim = c(4, 3, 2))
crop(a, i = 1, j = 2:3)a <- marray(1:8)
sub_arys <- array_split(a, indices_or_sections = 3)
sub_arys <- array_split(a, indices_or_sections = c(3, 5, 6))
a <- marray(1:12, dim = c(4, 3))
sub_arys <- array_split(a, indices_or_sections = 2)
sub_arys <- array_split(a, indices_or_sections = 2, axis = 2L)
a <- marray(1:24, dim = c(4, 3, 2), order = "F")
sub_arys <- array_split(a, indices_or_sections = 2)
sub_arys <- array_split(a, indices_or_sections = 2, axis = 2L)a <- marray(sample(7 * 4 * 2), dim = c(7, 4, 2))
# Sliding along each axes with a size of 1 and a stride of 1
sub_arys <- slide(a)
# Sliding along axes 1 and 2 with a size of 2 resp. 1 and a stride of 1
sub_arys <- slide(a, size = c(2, 1), axis = c(1, 2))a <- marray(1:(3*4*5), dim = c(3, 4, 5), order = "F")
DIM(moveaxis(a, 1, 3))
DIM(moveaxis(a, 3, 1))
DIM(moveaxis(a, source = c(1, 2), destination = c(3, 1)))
a <- marray(1:24, dim = c(4, 3, 2, 1))
DIM(moveaxis(a, source = c(1, 4), destination = c(3, 2)))
m <- marray(1:12, dim = c(4, 3))
DIM(moveaxis(m, 1, 2)) # transpose(m)