convert list to incidence matrix
Arguments
- x
listof item vectors, where vector values are item names- empty
numericused for missing or empty values, default 0.- do_sparse
logicaldefault FALSE, whether to return sparse Matrix. Whendo_sparse=TRUE, it setskeepCounts=FALSEsince a sparse Matrix does not represent counts.- keepCounts
logicaldefault FALSE, whether to include the count of any duplicated items.- sort_rows
logicaldefault FALSE, whether to sort rows usingjamba::mixedOrder(), orfunctioncan be supplied to use another sort algorithm.- ...
additional arguments are ignored.
Value
numeric matrix with values 0 or 1 indicating absense or
presence of each item (row) in each set (column).
The sets are derived from names(x).
Details
This function converts a list of vectors into an incidence matrix, where the rows are the vector items and the columns are the list names.
See also
Other jam list functions:
colors_from_list(),
im2list(),
imSigned2list(),
list2concordance(),
list2imSigned()
Examples
L1 <- list(A=c("C","A","B","A"),
D=c("D","E","F","D"),
A123=c(1:8,3,5),
B=c("A1", "A2", "A10"),
T=LETTERS[7:9]);
# Default behavior is to make items unique
list2im(L1);
#> A D A123 B T
#> C 1 0 0 0 0
#> A 1 0 0 0 0
#> B 1 0 0 0 0
#> D 0 1 0 0 0
#> E 0 1 0 0 0
#> F 0 1 0 0 0
#> 1 0 0 1 0 0
#> 2 0 0 1 0 0
#> 3 0 0 1 0 0
#> 4 0 0 1 0 0
#> 5 0 0 1 0 0
#> 6 0 0 1 0 0
#> 7 0 0 1 0 0
#> 8 0 0 1 0 0
#> A1 0 0 0 1 0
#> A2 0 0 0 1 0
#> A10 0 0 0 1 0
#> G 0 0 0 0 1
#> H 0 0 0 0 1
#> I 0 0 0 0 1
# Option to report the counts
list2im(L1, keepCounts=TRUE);
#> A D A123 B T
#> C 1 0 0 0 0
#> A 2 0 0 0 0
#> B 1 0 0 0 0
#> D 0 2 0 0 0
#> E 0 1 0 0 0
#> F 0 1 0 0 0
#> 1 0 0 1 0 0
#> 2 0 0 1 0 0
#> 3 0 0 2 0 0
#> 4 0 0 1 0 0
#> 5 0 0 2 0 0
#> 6 0 0 1 0 0
#> 7 0 0 1 0 0
#> 8 0 0 1 0 0
#> A1 0 0 0 1 0
#> A2 0 0 0 1 0
#> A10 0 0 0 1 0
#> G 0 0 0 0 1
#> H 0 0 0 0 1
#> I 0 0 0 0 1
# Option to sort item rows with jamba::mixedOrder()
list2im(L1, sort_rows=TRUE);
#> A D A123 B T
#> 1 0 0 1 0 0
#> 2 0 0 1 0 0
#> 3 0 0 1 0 0
#> 4 0 0 1 0 0
#> 5 0 0 1 0 0
#> 6 0 0 1 0 0
#> 7 0 0 1 0 0
#> 8 0 0 1 0 0
#> A1 0 0 0 1 0
#> A2 0 0 0 1 0
#> A10 0 0 0 1 0
#> A 1 0 0 0 0
#> B 1 0 0 0 0
#> C 1 0 0 0 0
#> D 0 1 0 0 0
#> E 0 1 0 0 0
#> F 0 1 0 0 0
#> G 0 0 0 0 1
#> H 0 0 0 0 1
#> I 0 0 0 0 1
# Option to sort item rows with vanilla sort()
list2im(L1, sort_rows=sort);
#> A D A123 B T
#> 1 0 0 1 0 0
#> 2 0 0 1 0 0
#> 3 0 0 1 0 0
#> 4 0 0 1 0 0
#> 5 0 0 1 0 0
#> 6 0 0 1 0 0
#> 7 0 0 1 0 0
#> 8 0 0 1 0 0
#> A 1 0 0 0 0
#> A1 0 0 0 1 0
#> A10 0 0 0 1 0
#> A2 0 0 0 1 0
#> B 1 0 0 0 0
#> C 1 0 0 0 0
#> D 0 1 0 0 0
#> E 0 1 0 0 0
#> F 0 1 0 0 0
#> G 0 0 0 0 1
#> H 0 0 0 0 1
#> I 0 0 0 0 1