user3179350
user3179350

Reputation: 75

Adjacency matrix - edges based on arbitrary variable

I'm trying to generate an adjacency matrix (later to be used to create a social network graph) from a simple three-variable data set. See below.

Year  Company  Project
2001    A       Proj1
2001    B       Proj1
2005    C       Proj3
2004    D       Proj4
2004    E       Proj4

The idea is to generate a matrix in which the rows and columns are companies and the edges indicate whether or not any given pair of companies has participated on the same project. This would look something like:

  A B C D E
A 0 1 0 0 0 
B 1 0 0 0 0
C 0 0 0 0 0
D 0 0 0 0 1
E 0 0 0 1 0

How would I accomplish something like this in R? Based on other users' attempts, I tried reshape2, but no luck. Any help would be appreciated. Thanks!

Upvotes: 1

Views: 201

Answers (1)

Tyler Rinker
Tyler Rinker

Reputation: 109864

There are better approaches but this is one I know. You'll need the development version of qdap. Get it by following the directions found at the bottom of THIS PAGE.

## dat <- read.table(text="Year  Company  Project
## 2001    A       Proj1
## 2001    B       Proj1
## 2005    C       Proj3
## 2004    D       Proj4
## 2004    E       Proj4", header=TRUE)

library(qdap)
x <- adjmat(mtabulate(split(dat$Company, dat$Project)))
x$adjacency

## > x$adjacency
##   A B C D E
## A 1 1 0 0 0
## B 1 1 0 0 0
## C 0 0 1 0 0
## D 0 0 0 1 1
## E 0 0 0 1 1

Admittedly that's a lot to install for this problem. It can be done with the reshape2 package as well and someone's sure to give that solution as well.

EDIT

No need for reshape here's the better way...

x <- table(dat[, -1])
x %*% t(x)

##        Company
## Company A B C D E
##       A 1 1 0 0 0
##       B 1 1 0 0 0
##       C 0 0 1 0 0
##       D 0 0 0 1 1
##       E 0 0 0 1 1

and if you really want zeros on the diagonals:

y <- x %*% t(x)
diag(y) <- 0
y

Upvotes: 2

Related Questions