collect {pkgutils} | R Documentation |
Methods for collecting information from list-like objects into a matrix or data frame or for re-assigning values to columns in a matrix.
collect(x, what, ...) ## S3 method for class 'list' collect(x, what = c("counts", "occurrences", "values", "elements", "datasets", "rows"), min.cov = 1L, keep.unnamed = FALSE, dataframe = FALSE, optional = TRUE, stringsAsFactors = default.stringsAsFactors(), ...) ## S3 method for class 'matrix' collect(x, what = c("columns", "rows"), empty = "?", ...)
x |
List or matrix. |
what |
Character scalar indicating how to collect information. The following values are supported by the list method:
The matrix method currently only
supports |
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 |
empty |
Character scalar used as intermediary placeholder for empty and missing values. |
... |
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 base::rbind
Other coding-functions: L
,
LL
, assert
,
case
, check
, contains
,
flatten
, listing
,
map_names
, map_values
,
must
, set
, sql
,
unnest
## 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))