Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Complex rearrangement of list into matrix

Tags:

list

r

matrix

Sorry for the vague title. Also, an example is worth a thousand words.

I have a list:

> lst<-list(A=c("one","two", "three"), B=c("two", "four", "five"), C=c("six", "seven"), D=c("one", "five", "eight"))

> lst
$A
[1] "one"   "two"   "three"

$B
[1] "two"  "four" "five"

$C
[1] "six"   "seven"

$D
[1] "one"   "five"  "eight"

that I want to rearrange into the following matrix:

> m
      A B C D
one   1 0 0 1
two   1 1 0 0
three 1 0 0 0
four  0 1 0 0
five  0 1 0 1
six   0 0 1 0
seven 0 0 1 0
eight 0 0 0 1

where, basically, each coordinate represents presence (1) or absence (0) of each list value in each list element.

I tried messing with various combinations of as.data.frame(), unlist(), table() and melt(), with no success, so any pointers in the right direction would be very appreciated.

I guess my last resort would be a nested loop that iterates through the list elements and then assign a 0 or a 1 to the corresponding coordinate in the matrix, but it seems overly complicated.

for (...) { 
    for (...) {
        if (...) {
            var <- 1
        } else {
            var <- 0
        }
    }
}

Thank you!

like image 698
enricoferrero Avatar asked Sep 04 '13 16:09

enricoferrero


2 Answers

library(reshape2)

table(melt(lst))
#       L1
#value   A B C D
#  one   1 0 0 1
#  three 1 0 0 0
#  two   1 1 0 0
#  five  0 1 0 1
#  four  0 1 0 0
#  seven 0 0 1 0
#  six   0 0 1 0
#  eight 0 0 0 1
like image 153
eddi Avatar answered Oct 21 '22 21:10

eddi


Here's a fairly manual approach:

t(table(rep(names(lst), sapply(lst, length)), unlist(lst)))
#        
#         A B C D
#   eight 0 0 0 1
#   five  0 1 0 1
#   four  0 1 0 0
#   one   1 0 0 1
#   seven 0 0 1 0
#   six   0 0 1 0
#   three 1 0 0 0
#   two   1 1 0 0

And, stack also works!

table(stack(lst))
#        ind
# values  A B C D
#   eight 0 0 0 1
#   five  0 1 0 1
#   four  0 1 0 0
#   one   1 0 0 1
#   seven 0 0 1 0
#   six   0 0 1 0
#   three 1 0 0 0
#   two   1 1 0 0

Update 1

If you cared about the row and column orders, you could explicitly factor them before using table:

A <- stack(lst)
A$values <- factor(A$values, 
                   levels=c("one", "two", "three", "four", 
                            "five", "six", "seven", "eight"))
A$ind <- factor(A$ind, c("A", "B", "C", "D"))
table(A)

Update 2: Benchmarks!

Because benchmarks are fun... even when we are talking about microseconds... Go unlist!

set.seed(1)
vec <- sample(3:10, 50, replace = TRUE)
lst <- lapply(vec, function(x) sample(letters, x))
names(lst) <- paste("A", sprintf("%02d", sequence(length(lst))), sep = "")

library(reshape2)
library(microbenchmark)

R2 <- function() table(melt(lst))
S <- function() table(stack(lst))
U <- function() t(table(rep(names(lst), sapply(lst, length)), unlist(lst, use.names=FALSE)))

microbenchmark(R2(), S(), U())
# Unit: microseconds
#  expr       min        lq     median        uq       max neval
#  R2() 36836.579 37521.295 38053.9710 40213.829 45199.749   100
#   S()  1427.830  1473.210  1531.9700  1565.345  3776.860   100
#   U()   892.265   906.488   930.5575   945.326  1261.592   100
like image 40
A5C1D2H2I1M1N2O1R2T1 Avatar answered Oct 21 '22 22:10

A5C1D2H2I1M1N2O1R2T1