You can use table(df)
to create the two-mode adjacency matrix
tb <- table(df)
> tb
value
name a b c d
Greg 1 0 0 0
Mary 0 1 0 1
Sam 1 1 1 0
Tom 0 1 1 1
If you want to keep the order of names according to the sequence of names appearing in df
, then you can also use order()
to reorder the rows in ``tb`, i.e.,
> tb[order(match(rownames(tb),df$name)),]
value
name a b c d
Sam 1 1 1 0
Greg 1 0 0 0
Tom 0 1 1 1
Mary 0 1 0 1
DATA
df <- structure(list(name = structure(c(3L, 3L, 3L, 1L, 4L, 4L, 4L,
2L, 2L), .Label = c("Greg", "Mary", "Sam", "Tom"), class = "factor"),
value = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 4L, 2L, 4L), .Label = c("a",
"b", "c", "d"), class = "factor")), row.names = c(NA, -9L
), class = "data.frame")