| D2A | R Documentation |
D2A makes an array out of one column in a dataframe, with (by default) the remaining columns forming the array dimensions, in order. Its (almost) inverse is A2D.
You can choose which columns to use for the dimensions, and in which order, via the dim.cols argument. However, it often easier to subset the array by columns in the call, eg D2A( x[ cq( Year, Len, Count), data.col="Count"). Each unique value in an index column gets a "row" in the array. Combinations of indices that don't appear as rows in the input will become missing.value in the output. If a row has missing values in any index column, it is ignored.
Duplicated index rows in the data.frame are not advisable, and trigger a warning; I think the last value will be used, but I'm not sure.
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.
You can of course do vaguely similar things with base-R and perhaps with countless other packages too, but why not just use this? I do!
D2A(
df,
data.col,
dim.cols = names(df) %except% data.col,
missing.value = NA)
df |
data.frame |
dim.cols |
character vector saying which columns (in order) to use for array dimensions. Default is everything except |
data.col |
string saying which column should form the contents of the output |
missing.value |
what to put into the output for index-combinations that don't occur in the input. |
Array with length( dim.cols) dimensions, and appropriate dimnames.
Dataframe to array
A2D
grubbb <- expand.grid( xx=1:4, yy=2:3) # data.frame
grubbb$z <- with( grubbb, xx+10*yy)
D2A( grubbb, 'z')
# Let's remove some values, and change the order of array dims...
minigrubbb <- grubbb[ c( 1, 3, 4, 7),]
D2A( minigrubbb, 'z', dim.cols=cq( yy, xx))
# Don't have to use all columns
D2A( minigrubbb, 'z', dim.cols='xx')
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.