I am trying to populate an adjacency matrix with "1"s if the values of cells in a data frame column match. The location of the "1"s is based on corresponding values in the same row.
To be more precise: Data frame pat1
ID PATID SUB
1 2 2A
2 2 2B
3 3 2C
4 3 2D
I'd like to populate the matrix cells [2A,2B]
, [2B,2A]
, [2C,2D]
, and [2D,2C]
in an empty matrix patmat1
with the corresponding row/col. names with a "1" since PATID[1]=PATID[2]
, and PATID[3]=PATID[4]
, respectively.
The desired output would be matrix(data = c(0,1,0,0,1,0,0,0,0,0,0,1,0,0,1,0), nrow=4, byrow=T) with colnames <- rownames <- c("2A", "2B", "2C", "2D") In this example, the patmat1 dimension would be 4 4 (2A,2B,2C,2D).
I've searched but found no approach yet.
patmat1
? – akrunacast(pat1, ID~ID, value.var="SUB", length)
fromlibrary(reshape2)
– akrun