1

我想使用 R 从已知关系列表中生成关系矩阵。例如,使用以下数据集:

John Green
Mary Blue
Mary Red
John Blue

我想拥有 :

       Green Blue Red
John     1     1   0
Mary     0     1   1

我没有找到如何做到这一点。提前感谢您的任何建议。

4

1 回答 1

5

您可以使用table.

people <- c("John", "Mary", "Mary", "John")
cols <- c("Green", "Blue", "Red", "Blue")
df <- data.frame(people, cols)

table(df)

      cols
people Blue Green Red
  John    1     1   0
  Mary    1     0   1
于 2012-07-10T09:57:08.090 回答