0
1   Sam     a
2   Sam     b
3   Sam     c
4  Greg     a
5   Tom     b
6   Tom     c
7   Tom     d
8  Mary     b
9  Mary     d

Two-Mode Adjacency Matrix that I want to have : I tried as.matrix, frame2web but

     a b c d
Sam  1 1 1 0
Greg 1 0 0 0
Tom  0 1 1 1
Mary 0 1 0 1
Sotos
  • 51,121
  • 6
  • 32
  • 66
Gaara
  • 1

1 Answers1

0

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")
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81