R - weighted adjacency matrix of two columns

I have a dataset similar to the following:

AuthorID    ThreadID
   1           A
   2           A
   1           A
   2           A
   2           C
   3           B
   1           C
   4           B
   4           C
   4           C

where AuthorIDidentifies a specific author c ThreadID. Topics can contain posts from many authors, and authors can post in many threads.

I, after a weighted adjacency matrix in R, which I can use with the graph, shows the number of times a particular one was AuthorIDexchanged with another AuthorIDwithin ThreadID. Therefore, for this data, the matrix should look like this ( AuthorIDlike column and row headings):

   1  2  3  4
1  .  3  0  1
2  .  .  0  1
3  .  .  .  1
4  .  .  .  .

Thanks in advance!

+4
source share
1 answer

R. -, /

dd<-read.table(text="AuthorID    ThreadID
   1           A
   2           A
   1           A
   2           A
   2           C
   3           B
   1           C
   4           B
   4           C
   4           C
", header=T)

x <- xtabs(~ThreadID+AuthorID, unique(dd)); 
mm <- crossprod(x,x)
mm[lower.tri(mm, TRUE)] <- NA

        AuthorID
AuthorID  1  2  3  4
       1 NA  2  0  1
       2 NA NA  0  1
       3 NA NA NA  1
       4 NA NA NA NA

xtabs . unique, ( . crossprod, - . lower.tri, .

+6

Source: https://habr.com/ru/post/1654645/


All Articles