A2D: Array into dataframe

A2DR Documentation

Array into dataframe

Description

From an array (or matrix or vector) input, A2D produces a dataframe with one column per dimension of the input, plus a column for the contents, which will be called "response" unless you set the name.of.response argument. Its (almost) inverse is D2A.

The other columns will have names "D1", "D2", etc, unless either (i) the input has a named dimnames attribute, in which its names will be used, or (ii) the argument "add.names" is set to a character vector naming the dimensions. They will be numeric if If the input has any dimnames, then the latter's non-NULL elements will be used in place of 1,2,3,... etc for the entries in the corresponding columns.

Offarray

If you know you are dealing with an offarray object rather than a regular array, you can just call as.data.frame(<myoffar>,...) instead, for clarity. But if you do call A2D, all will be well (try it). OTOH, if you call base::array2DF(<myoffar>) then you generally don't get what you want.

Note

D2A and (something similar to) A2D used to be in my semi-secret handy2 package under slightly different names, but they are useful enough that I've moved them to mvbutils in 2025. The handy2 version (array.to.data.frame AKA a2d) made factor columns rather than character, and contained a lot of code. A2D is largely a wrapper for base::array2DF (qv), which didn't use to exist; however, A2D makes numeric columns where possible and uses names of dimnames to set column names if possible. This all makes it work better with tapply.

A2D and D2A are not strict inverses, because (i) if you start with a data.frame that lacks rows for some index combinations, those rows will still appear in the result, (ii) factor columns turn into character columns, and (iii) columns might get re-ordered.

Usage

A2D( x, name.of.response = "Value")

Arguments

x

array, matrix, or, vector, including offarray objects from the eponymous package.

name.of.response

what to call the output column that holds the array contents (as opposed to its dimensions).

Value

A data.frame, with one more column than there are dimensions to a.

See Also

D2A

Examples

grubbb <- expand.grid( xx=1:4, yy=2:3) # data.frame
grubbb$z <- with( grubbb, xx+10*yy)
D2A( grubbb, data.col='z')
A2D( D2A( grubbb, data.col='z'), name.of.response='zzzzzzz')
# ... how very interesting...

mvbutils documentation built on May 25, 2026, 5:09 p.m.

Related to A2D in mvbutils...