R - Corresponding rows and columns of matrices of different lengths

my problem at the moment is the following. I have a single-mode oriented edgelist representing pairs of participants participating in collaborative projects for a specific year, which might look like this:

projektleader   projectpartner  year
A               B               2005
A               C               2000
B               A               2002
...             ...             ...

Now I only need a subset for one specific year. Not all actors are active throughout the year, so the sizes of the subsets vary. For the next network analysis, I need a weighted and directional adjacency matrix, so I use the [network packet] option to create it. I first load it as a network object and then convert it to an adjacency matrix.

grants_00 <- subset(grants, (year_grant=2000), select = c(projectpartner, projectleader))
nw_00 <- network(grants_08to11[,1:2], matrix="edgelist", directed=TRUE) 
grants_00.adj <- as.matrix(nw_00, matrix.type = "adjacency")

The resulting matrix looks something like

     A    B    C    E    ...
A    0    1    1    0
B    1    0    0    0
...

. : , . , , . , . .

ATM : . Zero. - . , , .

- , ?

+2
1

, , .

, :

**** : , ? ****

library(network)
N <- 20
grants <- data.frame(
        projectleader  = sample(x=LETTERS[1:20],size=N,replace = TRUE),
        projectpartner = sample(x=LETTERS[1:20],size=N,replace = TRUE),
        year_grant     = sample(x=0:5          ,size=N,replace = TRUE) +2000
)


head(grants)
  projectleader projectpartner year_grant
1             D              K       2002
2             M              M       2001
3             K              L       2005
4             N              Q       2002
5             G              D       2003
6             I              B       2004

## 
adjency <- function(year){
  grants_00 <- subset(grants, (year_grant==year),
        select = c(projectpartner, projectleader))
  nw_00 <- network(grants_00, matrix="edgelist", directed=TRUE) 
  grants_00.adj <- as.matrix(nw_00, matrix.type = "adjacency")
  as.data.frame(grants_00.adj)
}

plyr

library(plyr)
years <- unique(grants$year_grant)
years <- years[order(years)]
bigMatrix <- llply(as.list(years),.fun=adjm)

()

# create an empty matrix with NAs
population <- union(grants$projectpartner,grants$projectleader)
population_size <- length(population)
full_matrix <- matrix(rep(NA, population_size*population_size), 
       nrow=population_size)
rownames(full_matrix) <- colnames(full_matrix) <- population

,

frn <- as.matrix(bigMatrix[[1]])

tmp <- match(rownames(frn), rownames(full_matrix))
tmp2 <- match(colnames(frn), colnames(full_matrix))

# do a merge
full_matrix[tmp,tmp2] <- frn



head(bigMatrix[[1]])
  D I J K O Q S
D 0 0 0 0 0 0 0
I 0 0 0 0 0 0 0
J 1 0 0 0 0 0 0
K 0 0 0 0 0 0 0
O 0 0 0 1 0 0 0
Q 0 1 0 0 0 0 0

    K  M  L  Q  D  B  E  J  C  S  O  F  G  N  I  A  H
K  0 NA NA  0  0 NA NA  0 NA  0  0 NA NA NA  0 NA NA
M NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
L NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
Q  0 NA NA  0  0 NA NA  0 NA  0  0 NA NA NA  1 NA NA
D  0 NA NA  0  0 NA NA  0 NA  0  0 NA NA NA  0 NA NA
B NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
E NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
J  0 NA NA  0  1 NA NA  0 NA  0  0 NA NA NA  0 NA NA
C NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
S  0 NA NA  1  0 NA NA  0 NA  0  0 NA NA NA  0 NA NA
O  1 NA NA  0  0 NA NA  0 NA  0  0 NA NA NA  0 NA NA
F NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
G NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
N NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
I  0 NA NA  0  0 NA NA  0 NA  0  0 NA NA NA  0 NA NA
A NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
H NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
+4

All Articles