flatten {pkgutils} | R Documentation |
Methods for making an object ‘flat’, such as creating a non-nested list from a list, and methods for collecting information from list-like objects into a matrix or data frame.
flatten(object, ...) ## Default S3 method: flatten(object, ...) ## S3 method for class 'list' flatten(object, use.names = TRUE, ...) collect(x, what, ...) ## S3 method for class 'list' collect(x, what = c("counts", "occurrences", "values", "elements", "datasets"), min.cov = 1L, keep.unnamed = FALSE, dataframe = FALSE, optional = TRUE, stringsAsFactors = default.stringsAsFactors(), ...)
object |
Usually a list. The default method just
returns |
use.names |
Logical scalar passed to |
x |
List. |
what |
Character scalar indicating how to collect information.
|
min.cov |
Numeric scalar indicating the minimal coverage required in the resulting presence-absence matrix. Columns with a fewer number of non-zero entries are removed. |
keep.unnamed |
Logical scalar indicating whether
names should be inserted for elements of |
dataframe |
Logical scalar indicating whether a data frame should be produced instead of a matrix. |
optional |
See |
stringsAsFactors |
See |
... |
Optional arguments passed to and from other
methods (if requested to |
The list method of flatten
is based on
http://stackoverflow.com/questions/8139677/ with
some slight improvements.
The list method of flatten
returns a non-nested
list. The collect
methods yield a data frame or a
matrix.
base::unlist base::as.data.frame
Other coding-functions: L
,
LL
, case
,
contains
, listing
,
map_names
, map_values
,
must
## flatten()
x <- list(a = list(b = 1:5, c = letters[1:5]), d = LETTERS[1:3],
e = list(pi))
(y <- flatten(x)) # sublists removed, non-list elements kept
## $a.b
## [1] 1 2 3 4 5
##
## $a.c
## [1] "a" "b" "c" "d" "e"
##
## $d
## [1] "A" "B" "C"
##
## $e
## [1] 3.141593
stopifnot(is.list(y), length(y) == 4, !sapply(y, is.list))
# atomic objects are not modified by default
stopifnot(identical(letters, flatten(letters)))
## collect()
x <- list(X = list(A = 1:3, B = 7L, C = list('c1', 1:3)),
Y = list(A = 1:3, 11, B = -1L, D = "?"))
## collect values into a data frame or matrix
(got <- collect(x, "values", dataframe = TRUE))
## A B C D
## X 1, 2, 3 7 c1, 1, 2, 3 <NA>
## Y 1, 2, 3 -1 NA ?
stopifnot(LETTERS[1:4] == colnames(got))
stopifnot(names(x) == rownames(got))
stopifnot(is.list(got$A), is.integer(got$B), is.list(got$C),
is.factor(got$D))
stopifnot(!is.na(got$A), !is.na(got$B), anyNA(got$C), anyNA(got$D))
# include the unnamed ones
got <- collect(x, "values", dataframe = TRUE, keep.unnamed = TRUE)
stopifnot(dim(got) == c(2, 5))
# simplify to matrix
(got <- collect(x, "values", dataframe = FALSE))
## A B C D
## X Integer,3 7 List,2 "NA"
## Y Integer,3 -1 NA "?"
stopifnot(is.matrix(got), mode(got) == "list")
## collect elements into a data frame or matrix
(got <- collect(x, "elements", dataframe = TRUE))
## A1 A2 A3 B C1 C2 C3 C4 D
## X 1 2 3 7 c1 1 2 3 <NA>
## Y 1 2 3 -1 <NA> <NA> <NA> <NA> ?
stopifnot(dim(got) == c(2, 9), colnames(x) == rownames(got),
is.data.frame(got))
(got <- collect(x, "elements", dataframe = FALSE))
## A1 A2 A3 B C1 C2 C3 C4 D
## X "1" "2" "3" "7" "c1" "1" "2" "3" NA
## Y "1" "2" "3" "-1" NA NA NA NA "?"
stopifnot(dim(got) == c(2, 9), colnames(x) == rownames(got),
!is.data.frame(got))
## count or just note occurrences
(got <- collect(x, "counts", dataframe = FALSE))
## -1 1 11 2 3 7 ? c1
## X 0 2 0 2 2 1 0 1
## Y 1 1 1 1 1 0 1 0
stopifnot(dim(got) == c(2, 8), rownames(got) == names(x),
setequal(colnames(got), unlist(x)), any(got > 1))
(got <- collect(x, "occurrences", dataframe = FALSE))
## -1 1 11 2 3 7 ? c1
## X 0 1 0 1 1 1 0 1
## Y 1 1 1 1 1 0 1 0
stopifnot(dim(got) == c(2, 8), rownames(got) == names(x),
setequal(colnames(got), unlist(x)), !any(got > 1))
## convert to data frames and insert everything in a single one
(got <- collect(x, "datasets", optional = FALSE, dataframe = TRUE))
## A B C..c1. C.1.3 D X11
## 1 1 -1 c1 1 ? 11
## 2 2 -1 c1 2 ? 11
## 3 3 -1 c1 3 ? 11
stopifnot(dim(got) == c(3, 6), is.data.frame(got))
## a more useful application is to merge matrices
m1 <- matrix(1:4, ncol = 2, dimnames = list(c("A", "B"), c("x", "y")))
m2 <- matrix(1:4, ncol = 2, dimnames = list(c("C", "B"), c("x", "z")))
(got <- collect(list(m1, m2), "datasets"))
## x y z
## A 1 3 NA
## B 2 4 4
## C 1 NA 3
# values missing in some matrix yield NA
stopifnot(dim(got) == c(3, 3), anyNA(got))