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

d2a

Data-Frame to (3D+) Array


Description

d2a converts a data.frame to a (3D+) array. This function assumes the data.frame contains 3+ variable dimensions, which will correspond to the returned arrays dimensions. One variable (and one variable only) must contain the elements which will correspond to the elements of the returned array.

Usage

d2a(d, el.nm = "element", check = TRUE)

Arguments

d

data.frame with at least 4 columns, where 3+ columns are variable dimensions and 1 column contains the to-be array elements. The columns containing the variable dimensions do not need to be factors or character vectors.

el.nm

character vector of length 1 specifying the colname in d that contains the to-be array elements.

check

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

Details

d2a is a wrapper for reshape::cast with the addition of reordering the dimnames by position, which sorts the dimnames by the position they first appear in the variable dimensions of the data.frame (reshape::cast) sorts all the dimnames alphabetically).

Value

(3D+) array containing the elements in d[[el.nm]]. The dimlabels are the colnames of d other than el.nm and the dimnames are the unique elements in those columns. The dimnames are ordered by position (rather than alphabetically), which allow for conversions back to the original array after a call to a2d().

Examples

print(HairEyeColor)
d <- reshape::melt.array(HairEyeColor)
a <- reshape::cast(d, Hair ~ Eye ~ Sex)
identical(a, unclass(HairEyeColor)) # not the same as HairEyeColor
d <- a2d(HairEyeColor)
a <- d2a(d)
identical(a, unclass(HairEyeColor)) # yes the same as HairEyeColor

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.