0

I have a df in which every columns represent an event and in cells there are the individuals, like this:

df=data.frame(topic1=c("a", "b","c", "d"), topic2=c("e","f", "g", "a"), topic3=c("b","c","g","h"))

I need to transform it in adjacency df, like this:

        topic1 topic2   topic3
   a    1      1        0
   b    1      0        1
   c    1      0        1
   d    1      0        0
   e    0      1        0
   f    0      1        0
   g    0      1        1
   h    0      0        1

THX!

ErreEmme
  • 17
  • 4

1 Answers1

1

Form levs containing the levels in sorted order and then for each column of df determine which levs are in it. This gives a logical matrix which we can convert to numeric using +.

levs <- sort(unique(unlist(df)))   # a b c d e f g h
+ sapply(df, function(x) levs %in% x)

giving:

     topic1 topic2 topic3
[1,]      1      1      0
[2,]      1      0      1
[3,]      1      0      1
[4,]      1      0      0
[5,]      0      1      0
[6,]      0      1      0
[7,]      0      1      1
[8,]      0      0      1

The last line could be written even more compactly as:

+ sapply(df, `%in%`, x = levs)
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341