非排他性因素的指标矩阵

时间:2017-01-16 06:01:31

标签: r

说我有一个包含因子矢量的列表:

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

可能如下所示:

> 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

如何构建表示相同信息的指标值矩阵:

     [,1] [,2] [,3]
[1,]    0    1    1
[2,]    1    0    0
[3,]    1    1    1
[4,]    1    1    1
[5,]    0    0    1

1 个答案:

答案 0 :(得分:2)

我们可以使用table

t(sapply(rgb, table))