2

Say I have a list containing vectors of factors:

colors <- c("red", "green", "blue")
f <- factor(colors, levels=colors)
rgb <- sapply(sample(1:3, 5, replace=TRUE), function(n) sample(f, n))

Which might look like this:

> invisible(sapply(rgb, function(x) print(x, max.levels=0)))
[1] blue  green
[1] red
[1] blue  red   green
[1] red   blue  green
[1] blue

How can I construct a matrix of indicator values that represent the same information:

     [,1] [,2] [,3]
[1,]    0    1    1
[2,]    1    0    0
[3,]    1    1    1
[4,]    1    1    1
[5,]    0    0    1
user12341234
  • 6,573
  • 6
  • 23
  • 48
  • 1
    While my question may be similar to the one linked, I believe @akrun's solves my problem significantly more simply than any of the answers in the other question. – user12341234 Jan 16 '17 at 06:46

1 Answers1

2

We can use table

t(sapply(rgb, table))
akrun
  • 874,273
  • 37
  • 540
  • 662