| 84178 |
deepayan |
1 |
% File src/library/base/man/array2DF.Rd
|
|
|
2 |
% Part of the R package, https://www.R-project.org
|
|
|
3 |
% Copyright 2023 R Core Team
|
|
|
4 |
% Distributed under GPL 2 or later
|
|
|
5 |
|
|
|
6 |
\name{array2DF}
|
|
|
7 |
\title{Convert array to data frame}
|
|
|
8 |
\alias{array2DF}
|
|
|
9 |
|
|
|
10 |
\description{
|
|
|
11 |
\code{array2DF} converts an array, including list arrays commonly
|
|
|
12 |
returned by \code{tapply}, into data frames for use in further
|
|
|
13 |
analysis or plotting functions.
|
|
|
14 |
}
|
|
|
15 |
\usage{
|
|
|
16 |
array2DF(x, responseName = "Value",
|
|
|
17 |
sep = "", base = list(LETTERS),
|
| 84254 |
deepayan |
18 |
simplify = TRUE, allowLong = TRUE)
|
| 84178 |
deepayan |
19 |
}
|
|
|
20 |
\arguments{
|
|
|
21 |
\item{x}{an array object.}
|
|
|
22 |
\item{responseName}{character string, used for creating column name(s)
|
|
|
23 |
in the result, if required. }
|
|
|
24 |
\item{sep}{character string, used as separator when creating new
|
|
|
25 |
names, if required. }
|
|
|
26 |
\item{base}{character vector, giving an initial set of names to create
|
|
|
27 |
dimnames of \code{x}, if missing. }
|
|
|
28 |
\item{simplify}{logical, whether to attempt simplification of the
|
|
|
29 |
result. }
|
|
|
30 |
\item{allowLong}{logical, specifying whether a long format data frame
|
|
|
31 |
should be returned if \code{x} is a list array and all elements of
|
|
|
32 |
\code{x} are unnamed atomic vectors. Ignored unless \code{simplify =
|
|
|
33 |
TRUE}. }
|
|
|
34 |
}
|
|
|
35 |
\details{
|
|
|
36 |
The main use of \code{array2DF} is to convert an array, as typically
|
|
|
37 |
returned by \code{\link{tapply}}, into a data frame.
|
|
|
38 |
|
| 84254 |
deepayan |
39 |
When \code{simplify = FALSE}, this is similar to
|
| 84178 |
deepayan |
40 |
\code{\link{as.data.frame.table}}, except that it works for list
|
|
|
41 |
arrays as well as atomic arrays. Specifically, the resulting data
|
|
|
42 |
frame has one row for each element of the array, with one column for
|
|
|
43 |
each dimension of the array giving the corresponding
|
|
|
44 |
\code{\link{dimnames}}. The contents of the array are placed in a
|
|
|
45 |
column whose name is given by the \code{responseName} argument. The
|
|
|
46 |
mode of this column is the same as that of \code{x}, usually an atomic
|
|
|
47 |
vector or a list.
|
|
|
48 |
|
|
|
49 |
If \code{x} does not have \code{\link{dimnames}}, they are
|
|
|
50 |
automatically created using \code{base} and \code{sep}.
|
|
|
51 |
|
| 84254 |
deepayan |
52 |
In the default case, when \code{simplify = TRUE}, some common cases
|
|
|
53 |
are handled specially.
|
| 84178 |
deepayan |
54 |
|
|
|
55 |
If all components of \code{x} are data frames with identical column
|
|
|
56 |
names (with possibly different numbers of rows), they are
|
|
|
57 |
\code{\link{rbind}}-ed to form the response. The additional columns
|
|
|
58 |
giving \code{dimnames} are repeated according to the number of
|
|
|
59 |
rows, and \code{responseName} is ignored in this case.
|
|
|
60 |
|
|
|
61 |
If all components of \code{x} are \emph{unnamed} atomic vectors
|
|
|
62 |
\emph{and} \code{allowLong = TRUE}, each component is treated as a
|
|
|
63 |
single-column data frame with column name given by
|
|
|
64 |
\code{responseName}, and processed as above.
|
|
|
65 |
|
|
|
66 |
In all other cases, an attempt to simplify is made by
|
|
|
67 |
\code{\link{simplify2array}}. If this results in multiple unnamed
|
|
|
68 |
columns, names are constructed using \code{responseName} and
|
|
|
69 |
\code{sep}.
|
|
|
70 |
|
|
|
71 |
}
|
|
|
72 |
\value{
|
|
|
73 |
A data frame with at least \code{length(dim(x)) + 1} columns. The
|
|
|
74 |
first \code{length(dim(x))} columns each represent one dimension of
|
|
|
75 |
\code{x} and gives the corresponding values of \code{dimnames}, which
|
|
|
76 |
are implicitly created if necessary. The remaining columns contain the
|
|
|
77 |
contents of \code{x}, after attempted simplification if requested.
|
|
|
78 |
}
|
|
|
79 |
\seealso{
|
|
|
80 |
\code{\link{tapply}}, \code{\link{as.data.frame.table}},
|
|
|
81 |
\code{\link{split}}, \code{\link{aggregate}}.
|
|
|
82 |
}
|
|
|
83 |
\examples{
|
|
|
84 |
s1 <- with(ToothGrowth,
|
|
|
85 |
tapply(len, list(dose, supp), mean, simplify = TRUE))
|
|
|
86 |
|
|
|
87 |
s2 <- with(ToothGrowth,
|
|
|
88 |
tapply(len, list(dose, supp), mean, simplify = FALSE))
|
|
|
89 |
|
|
|
90 |
str(s1) # atomic array
|
|
|
91 |
str(s2) # list array
|
|
|
92 |
|
| 84254 |
deepayan |
93 |
str(array2DF(s1, simplify = FALSE)) # Value column is vector
|
|
|
94 |
str(array2DF(s2, simplify = FALSE)) # Value column is list
|
|
|
95 |
str(array2DF(s2, simplify = TRUE)) # simplified to vector
|
| 84178 |
deepayan |
96 |
|
| 84254 |
deepayan |
97 |
### The remaining examples use the default 'simplify = TRUE'
|
| 84178 |
deepayan |
98 |
|
| 84254 |
deepayan |
99 |
## List array with list components: columns are lists (no simplification)
|
|
|
100 |
|
| 84178 |
deepayan |
101 |
with(ToothGrowth,
|
|
|
102 |
tapply(len, list(dose, supp),
|
|
|
103 |
function(x) t.test(x)[c("p.value", "alternative")])) |>
|
| 84254 |
deepayan |
104 |
array2DF() |> str()
|
| 84178 |
deepayan |
105 |
|
| 84254 |
deepayan |
106 |
## List array with data frame components: columns are atomic (simplified)
|
| 84178 |
deepayan |
107 |
|
|
|
108 |
with(ToothGrowth,
|
|
|
109 |
tapply(len, list(dose, supp),
|
|
|
110 |
function(x) with(t.test(x), data.frame(p.value, alternative)))) |>
|
| 84254 |
deepayan |
111 |
array2DF() |> str()
|
| 84178 |
deepayan |
112 |
|
|
|
113 |
## named vectors
|
|
|
114 |
|
|
|
115 |
with(ToothGrowth,
|
|
|
116 |
tapply(len, list(dose, supp),
|
| 84254 |
deepayan |
117 |
quantile)) |> array2DF()
|
| 84178 |
deepayan |
118 |
|
|
|
119 |
## unnamed vectors: long format
|
|
|
120 |
|
|
|
121 |
with(ToothGrowth,
|
|
|
122 |
tapply(len, list(dose, supp),
|
| 84254 |
deepayan |
123 |
sample, size = 5)) |> array2DF()
|
| 84178 |
deepayan |
124 |
|
|
|
125 |
## unnamed vectors: wide format
|
|
|
126 |
|
|
|
127 |
with(ToothGrowth,
|
|
|
128 |
tapply(len, list(dose, supp),
|
| 84254 |
deepayan |
129 |
sample, size = 5)) |> array2DF(allowLong = FALSE)
|
| 84178 |
deepayan |
130 |
|
|
|
131 |
## unnamed vectors of unequal length
|
|
|
132 |
|
|
|
133 |
with(ToothGrowth[-1, ],
|
|
|
134 |
tapply(len, list(dose, supp),
|
|
|
135 |
sample, replace = TRUE)) |>
|
| 84254 |
deepayan |
136 |
array2DF(allowLong = FALSE)
|
| 84178 |
deepayan |
137 |
|
|
|
138 |
## unnamed vectors of unequal length with allowLong = TRUE
|
|
|
139 |
## (within-group bootstrap)
|
|
|
140 |
|
|
|
141 |
with(ToothGrowth[-1, ],
|
|
|
142 |
tapply(len, list(dose, supp), sample, replace = TRUE)) |>
|
| 84254 |
deepayan |
143 |
array2DF() |> str()
|
| 84178 |
deepayan |
144 |
|
|
|
145 |
## data frame input
|
|
|
146 |
|
|
|
147 |
tapply(ToothGrowth, ~ dose + supp, FUN = with,
|
|
|
148 |
data.frame(n = length(len), mean = mean(len), sd = sd(len))) |>
|
| 84254 |
deepayan |
149 |
array2DF()
|
| 84178 |
deepayan |
150 |
|
|
|
151 |
}
|
|
|
152 |
\keyword{array}
|