flatten_array: Flatten a 3D array into a 2D data frame.

View source: R/array_fun.R

flatten_arrayR Documentation

Flatten a 3D array into a 2D data frame.

Description

flatten_array turns a 3-dimensional array into a 2-dimensional data frame (in wide format).

Usage

flatten_array(x, margin = 2, varsAsFactors = FALSE)

Arguments

x

A 3-dimensional array.

margin

The margin across which c is to be applied. Default: margin = 2 (i.e., columns).

varsAsFactors

Boolean: Should reconstructed variables be factors? Default: varsAsFactors = FALSE (i.e., character variables).

Details

flatten_array assumes that x is a 3-dimensional array with dimension names (and calls add_dimnames if they are absent).

The margin argument specifies an array dimension to be passed to apply(x, MARGIN = margin, FUN = c).

flatten_array returns NA for non-arrays and for arrays with more than 3 dimensions.

As R objects of class table are also arrays (contingency tables of frequency counts, in numeric mode) flatten_array works for them as well.

To flatten numeric arrays or objects of class table with more then 3 dimensions, use margin.table (from base R) to create a 3-dimensional aggregate first.

Internally, flatten_array uses apply to apply the c function to a specified margin of x. It aims to reconstruct the names of the collapsed variables from the initial letters of the dimension names.

See ftable (from the stats package) for a more general function (in combination with aperm) and margin.table (from base R) and addmargins (from stats) for aggregating over table dimensions.

Value

A data frame.

See Also

ftable for flattening multi-dimensional arrays; margin.table for aggregating across array dimensions; addmargins for expanding factor levels on margins; aperm for permuting array dimensions; add_dimnames for adding dimension names to arrays.

Other array functions: add_dimnames(), ctable(), expand_freq_table(), subtable()

Examples

a1 <- array(data = LETTERS[1:8], dim = c(2, 2, 2), 
            dimnames = list(c("r1", "r2"), c("c1", "c2"), c("t1", "t2")))
flatten_array(a1)  # using default (margin = 2) 

# Using names of dimnames:
names(dimnames(a1)) <- c("row", "col", "tab")
flatten_array(a1)
flatten_array(a1, margin = 3)  

# Returning name variables as factors:
a1f <- flatten_array(a1, varsAsFactors = TRUE)
is.factor(a1f$r)
is.factor(a1f$t)

a2 <- array(data = 1:60, dim = c(5, 4, 3))  # no dimnames 
flatten_array(a2)  # default names added
flatten_array(a2, margin = 1)   
flatten_array(a2, margin = 3)

a3 <- array(data = 1:2^4, dim = c(2, 2, 2, 2))  # 4-dimensions
flatten_array(a3)
flatten_array(margin.table(a3, margin = 1:3))

# For table:
# UCBAdmissions data (3-dimensions): 
flatten_array(UCBAdmissions) # margin = 2
flatten_array(UCBAdmissions, margin = 1)
flatten_array(UCBAdmissions, margin = 3)

# Titanic data (4-dimensions):
T3d <- margin.table(Titanic, margin = c(2, 3, 4))  # aggregate 3d-array
flatten_array(T3d, margin = 3)  # compare to ftable(T3d)


hneth/i2ds documentation built on Aug. 17, 2024, 9:31 p.m.