R - Matching rows and colums of matrices with different length

感情迁移 提交于 2019-12-06 10:28:02

问题


my problem at the moment is the following. I have an directed 1-mode edgelist representing pairs of actors participating in joint projects in a certain year, which might look like:

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

Now I need only a subset for one particular year. Not all actors are active in very year, so the dimensions of the subsets differ. For a following Network Analysis, I need a weighted and directed adjacency matrix, so I use the option of the [network package] to create it. I first load it as a network object and transform it then in a 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 somewhat like

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

So far so good. My problem is now: For the further analysis I am planning to do I need an adjacency Matrix for every year with the same dimension and order. That means that all actors from the initial dataset have to be the row and column names of the matrix for the corresponding years, but the matrix should only contain observed pairs for this certain year. I hope my problem is clear. I appreciate any kind of constructive solutions.

My idea ATM is the following: I create a matrix of the initial dataset and the reduced dataset. Then I set all matrix values there to Zero. Then I somehow match it with the reduced matrix and fill it with the right values in the right rows and columns. Unfortunately I have no clue how this might be possible.

Has anybody an idea how to solve this problem?


回答1:


Unfortunately , your question is not clear, so I will try to answer.

If I understand you want :

****Given a big and small matrix : Find the locations where they match?****

I regenerate your data

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

Function to create the small matrix

## 
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)
}

use plyr to get a list for every year

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

Create full matrix (The answer)

# 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

find the location where they match

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

the full matrix

    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


来源:https://stackoverflow.com/questions/13651402/r-matching-rows-and-colums-of-matrices-with-different-length

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!