Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

a2d

(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.

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. 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 has a column 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 names of the dimensions in a. If there were no dimension names 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 \code{m2d}.

str2str

Convert R Objects from One Structure to Another

v0.1.1
GPL (>= 2)
Authors
David Disabato [aut, cre]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.