flatten {pkgutils}R Documentation

Flatten a list or collect information from it

Description

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.

Usage

  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(), ...)

Arguments

object

Usually a list. The default method just returns object if it is atomic but raises an error otherwise.

use.names

Logical scalar passed to unlist from the base package.

x

List.

what

Character scalar indicating how to collect information.

counts

For all non-list elements of x, count their occurrences.

occurrences

Like ‘counts’, but only indicate presence or absence.

values

Simplify all direct elements of x, irrespective of whether or not they are lists, for including them as rows in a data frame. Their names determine the columns. See keep.unnamed for the action in the case of missing names.

elements

Like ‘elements’, but collect only the non-list elements of x, i.e. flatten x in the first step.

datasets

Convert all elements to data frames or matrices, then merge them using rows and column names. In case of conflict, the last ones win. Here, the behaviour of other arguments is special if all elements of x are atomic. See below.

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 x that miss them. If NA, they are skipped, but with a warning; if FALSE, they are skipped silently. This only has an effect in conjunction with the last three values of what. If datasets are chosen, it usually has only an effect if all elements of x are atomic.

dataframe

Logical scalar indicating whether a data frame should be produced instead of a matrix.

optional

See as.data.frame from the base package.

stringsAsFactors

See as.data.frame from the base package.

...

Optional arguments passed to and from other methods (if requested to as.data.frame).

Details

The list method of flatten is based on http://stackoverflow.com/questions/8139677/ with some slight improvements.

Value

The list method of flatten returns a non-nested list. The collect methods yield a data frame or a matrix.

See Also

base::unlist base::as.data.frame

Other coding-functions: L, LL, case, contains, listing, map_names, map_values, must

Examples

## 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))

[Package pkgutils version 0.7.5 Index]