list2matrix: Convert a list to an indicator or count matrix

Description Usage Arguments Details Author(s) Examples

Description

Convert a list of vectors into an indicator matrix

Usage

1
list2matrix(mylist, count = FALSE)

Arguments

mylist

List to convert to matrix

count

Do want a full count of items, or just an indicator matrix? Default value is FALSE; meaning just an indicator matrix.

Details

Converts a list of vectors into an indicator matrix that spans the universe of the list elements. Add count = TRUE to show how many times each item appears in each list element, otherwise it's a 1 or 0 indicator matrix.

This is optimized for large matricies when count = FALSE. The count feature should slow things down a bit for large data sets.

Author(s)

Gene Leynes

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
## Imagine you had shoppers who had bought multiple items, sometimes in
## multiples, and you wanted to pass the times into a model as a matrix.
ex <- list(ShopperNo1 = c("tomatoes", "apples", "oranges", "apples"),
           ShopperNo2 = c("watermellons", "apples", "mangos", "mangos", "mangos"),
           ShopperNo3 = c("carrots", "watermellons"),
           ShopperNo4 = c("mangos", "mangos", "mangos", "mangos"),
           ShopperNo5 = c("mangos"))
ex
## Indicator matrix (fast on large data sets)
list2matrix(ex)
## Count matrix shows how many times something occurred, not just if
## it occurred
list2matrix(ex, count = TRUE)


## This example is more confusing, but it can be interesting and useful to
## see package dependencies, espeically if you have a lot of interdependent
## packages loaded such as ggplot2, reshape2, data.table
deplist <- dependencylist(standard_packages = "")
list2matrix(deplist)
deplist

geneorama/geneorama documentation built on Oct. 17, 2020, 12:35 a.m.