-
Notifications
You must be signed in to change notification settings - Fork 292
Expand file tree
/
Copy pathtranspose.Rd
More file actions
74 lines (67 loc) · 2.58 KB
/
transpose.Rd
File metadata and controls
74 lines (67 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/superseded-transpose.R
\name{transpose}
\alias{transpose}
\title{Transpose a list.}
\usage{
transpose(.l, .names = NULL)
}
\arguments{
\item{.l}{A list of vectors to transpose. The first element is used as the
template; you'll get a warning if a subsequent element has a different
length.}
\item{.names}{For efficiency, \code{transpose()} bases the return structure on
the first component of \code{.l} by default. Specify \code{.names} to override this.}
}
\value{
A list with indexing transposed compared to \code{.l}.
\code{transpose()} is its own inverse, much like the transpose operation on a
matrix. You can get back the original input by transposing it twice.
}
\description{
\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#superseded}{\figure{lifecycle-superseded.svg}{options: alt='[Superseded]'}}}{\strong{[Superseded]}}
\code{transpose()} turns a list-of-lists "inside-out"; it turns a pair of lists
into a list of pairs, or a list of pairs into pair of lists. For example,
if you had a list of length n where each component had values \code{a} and
\code{b}, \code{transpose()} would make a list with elements \code{a} and
\code{b} that contained lists of length n. It's called transpose because
\code{x[[1]][[2]]} is equivalent to \code{transpose(x)[[2]][[1]]}.
This function was superseded in purrr 1.0.0 because \code{\link[=list_transpose]{list_transpose()}}
has a better name and can automatically simplify the output, as is commonly
needed. Superseded functions will not go away, but will only receive critical
bug fixes.
}
\examples{
x <- map(1:5, \(i) list(x = runif(1), y = runif(5)))
# was
x |> transpose() |> str()
# now
x |> list_transpose(simplify = FALSE) |> str()
# transpose() is useful in conjunction with safely() & quietly()
x <- list("a", 1, 2)
y <- x |> map(safely(log))
# was
y |> transpose() |> str()
# now:
y |> list_transpose() |> str()
# Previously, output simplification required a call to another function
x <- list(list(a = 1, b = 2), list(a = 3, b = 4), list(a = 5, b = 6))
x |> transpose() |> simplify_all()
# Now can take advantage of automatic simplification
x |> list_transpose()
# Provide explicit component names to prevent loss of those that don't
# appear in first component
ll <- list(
list(x = 1, y = "one"),
list(z = "deux", x = 2)
)
ll |> transpose()
nms <- ll |> map(names) |> reduce(union)
# was
ll |> transpose(.names = nms)
# now
ll |> list_transpose(template = nms)
# and can supply default value
ll |> list_transpose(template = nms, default = NA)
}
\keyword{internal}