a2d {str2str}R Documentation

(3D+) Array to Data-Frame

Description

a2d converts a (3D+ array) to a data.frame. It allows you to specify a dimension of the array to be the columns. All other dimensions are variables in the data.frame. This is different than as.data.frame.array which converts the (3D+) array to a matrix first; although it is very similar to as.data.frame.table when col = 0.

Usage

a2d(a, col = 0, stringsAsFactors = FALSE, check = TRUE)

Arguments

a

3D+ array.

col

integer vector or character vector of length 1 specifing the dimension of a to have as columns in the return object. If an integer vector, col refers to the dimension number. If a character vector, col refers to the name of the dimension (i.e., dimlabel). The columns are in order of the dimnames for that dimension (not alphabetical order like reshape::cast). If 0 (default), then no dimension of the array are columns and the function becomes similar to as.data.frame.table.

stringsAsFactors

logical vector of length 1 specifying whether the variable dimensions should be factors of chracter vectors.

check

logical vector of length 1 specifying whether to check the structure of the input arguments. For example, check whether a is a (3D+) array. This argument is available to allow flexibility in whether the user values informative error messages (TRUE) vs. computational efficiency (FALSE).

Details

a2d is mostly a wrapper for reshape::melt.array (+ reshape::cast) that allows for the variable dimensions to be character vectors rather than factors.

Value

data.frame of a's elements. The colnames of the variable dimensions are the dimlabels in a. If there were no dimlabels in a, then each dimension is named after its number with an X in front. If col is not 0, then the rest of the colnames are the dimnames of that dimension from a. If col is 0, then the names of the single column with a's elements is "element".

Examples


a2d(HairEyeColor)
a2d(HairEyeColor, col = 1)
a2d(HairEyeColor, col = "Hair", stringsAsFactors = TRUE)
a2d(HairEyeColor, col = 2)
a2d(HairEyeColor, col = "Sex", stringsAsFactors = TRUE)
try_expr(a2d(as.matrix(attitude))) # error due to inputting a matrix. Instead use `m2d`.

# correlation array example from psych::corr.test(attitude[1:3])
# corr_test <- psych::corr.test(attitude[1:3])
# a <- lm2a(corr_test[c("r","se","t","p")])
r <- matrix(c(1.0000000, 0.8254176, 0.4261169, 0.8254176, 1.0000000, 0.5582882,
   0.4261169, 0.5582882, 1.0000000), nrow = 3, ncol = 3, byrow = FALSE)
se <- matrix(c(0.0000000, 0.1066848, 0.1709662, 0.1066848, 0.0000000, 0.1567886,
   0.1709662, 0.1567886, 0.0000000), nrow = 3, ncol = 3, byrow = FALSE)
t <- matrix(c(Inf, 7.736978, 2.492404, 7.736978, Inf, 3.560771,
   2.492404, 3.560771, Inf), nrow = 3, ncol = 3, byrow = FALSE)
p <- matrix(c(0.000000e+00, 1.987682e-08, 1.887702e-02, 5.963047e-08, 0.000000e+00,
   1.345519e-03, 0.018877022, 0.002691039, 0.000000000), nrow = 3, ncol = 3, byrow = FALSE)
a <- abind::abind(r, se, t, p, along = 3L)
dimnames(a) <- list(names(attitude[1:3]), names(attitude[1:3]), c("r","se","t","p"))
d <- a2d(a = a, col = 3)


[Package str2str version 1.0.0 Index]