array2DF {base} | R Documentation |
Convert array to data frame
Description
array2DF
converts an array, including list arrays commonly
returned by tapply
, into data frames for use in further
analysis or plotting functions.
Usage
array2DF(x, responseName = "Value",
sep = "", base = list(LETTERS),
simplify = TRUE, allowLong = TRUE)
Arguments
x |
an array object. |
responseName |
character string, used for creating column name(s) in the result, if required. |
sep |
character string, used as separator when creating new names, if required. |
base |
character vector, giving an initial set of names to create
dimnames of |
simplify |
logical, whether to attempt simplification of the result. |
allowLong |
logical, specifying whether a long format data frame
should be returned if |
Details
The main use of array2DF
is to convert an array, as typically
returned by tapply
, into a data frame.
When simplify = FALSE
, this is similar to
as.data.frame.table
, except that it works for list
arrays as well as atomic arrays. Specifically, the resulting data
frame has one row for each element of the array, with one column for
each dimension of the array giving the corresponding
dimnames
. The contents of the array are placed in a
column whose name is given by the responseName
argument. The
mode of this column is the same as that of x
, usually an atomic
vector or a list.
If x
does not have dimnames
, they are
automatically created using base
and sep
.
In the default case, when simplify = TRUE
, some common cases
are handled specially.
If all components of x
are data frames with identical column
names (with possibly different numbers of rows), they are
rbind
-ed to form the response. The additional columns
giving dimnames
are repeated according to the number of
rows, and responseName
is ignored in this case.
If all components of x
are unnamed atomic vectors
and allowLong = TRUE
, each component is treated as a
single-column data frame with column name given by
responseName
, and processed as above.
In all other cases, an attempt to simplify is made by
simplify2array
. If this results in multiple unnamed
columns, names are constructed using responseName
and
sep
.
Value
A data frame with at least length(dim(x)) + 1
columns. The
first length(dim(x))
columns each represent one dimension of
x
and gives the corresponding values of dimnames
, which
are implicitly created if necessary. The remaining columns contain the
contents of x
, after attempted simplification if requested.
See Also
tapply
, as.data.frame.table
,
split
, aggregate
.
Examples
s1 <- with(ToothGrowth,
tapply(len, list(dose, supp), mean, simplify = TRUE))
s2 <- with(ToothGrowth,
tapply(len, list(dose, supp), mean, simplify = FALSE))
str(s1) # atomic array
str(s2) # list array
str(array2DF(s1, simplify = FALSE)) # Value column is vector
str(array2DF(s2, simplify = FALSE)) # Value column is list
str(array2DF(s2, simplify = TRUE)) # simplified to vector
### The remaining examples use the default 'simplify = TRUE'
## List array with list components: columns are lists (no simplification)
with(ToothGrowth,
tapply(len, list(dose, supp),
function(x) t.test(x)[c("p.value", "alternative")])) |>
array2DF() |> str()
## List array with data frame components: columns are atomic (simplified)
with(ToothGrowth,
tapply(len, list(dose, supp),
function(x) with(t.test(x), data.frame(p.value, alternative)))) |>
array2DF() |> str()
## named vectors
with(ToothGrowth,
tapply(len, list(dose, supp),
quantile)) |> array2DF()
## unnamed vectors: long format
with(ToothGrowth,
tapply(len, list(dose, supp),
sample, size = 5)) |> array2DF()
## unnamed vectors: wide format
with(ToothGrowth,
tapply(len, list(dose, supp),
sample, size = 5)) |> array2DF(allowLong = FALSE)
## unnamed vectors of unequal length
with(ToothGrowth[-1, ],
tapply(len, list(dose, supp),
sample, replace = TRUE)) |>
array2DF(allowLong = FALSE)
## unnamed vectors of unequal length with allowLong = TRUE
## (within-group bootstrap)
with(ToothGrowth[-1, ],
tapply(len, list(dose, supp), sample, replace = TRUE)) |>
array2DF() |> str()
## data frame input
tapply(ToothGrowth, ~ dose + supp, FUN = with,
data.frame(n = length(len), mean = mean(len), sd = sd(len))) |>
array2DF()