Skip to content
Prev 316864 / 398503 Next

adjacency list to non-symmetric matrix

Dear R community,

is there an easy way to convert an adjacency list (or a data-frame) to a non-symmetric matrix?

The adjacency list has the following form:

person group
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

I need the data in a matrix with persons as rows and groups as columns:

a b c d
Sam 1 1 1 0
Greg 1 0 0 0
Tom 0 1 1 1
Mary 0 1 0 1

I know that there are several possibilities in some of the network packages to convert adjacency lists to symmetric or sparse matrices, but I couldn't find a way to coerce them into affiliation matrices.


kind regards
Sebastian