r - Complex rearrangement of list into matrix -
sorry vague title. also, example worth thousand words.
i have 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 want rearrange following matrix:
> m b c d 1 1 0 0 1 2 1 1 0 0 3 1 0 0 0 4 0 1 0 0 5 0 1 0 1 6 0 0 1 0 7 0 0 1 0 8 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 various combinations of as.data.frame(), unlist(), table() , melt(), no success, pointers in right direction appreciated.
i guess last resort nested loop iterates through list elements , assign 0 or 1 corresponding coordinate in matrix, seems overly complicated.
for (...) { (...) { if (...) { var <- 1 } else { var <- 0 } } } thank you!
library(reshape2) table(melt(lst)) # l1 #value b c d # 1 1 0 0 1 # 3 1 0 0 0 # 2 1 1 0 0 # 5 0 1 0 1 # 4 0 1 0 0 # 7 0 0 1 0 # 6 0 0 1 0 # 8 0 0 0 1
Comments
Post a Comment