问题描述
目前,我的问题如下.我有一个有向的1模式边沿列表,代表在特定年份中参与联合项目的演员对,看起来可能像这样:
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
... ... ...
现在我只需要一个特定年份的子集.并非所有参与者都在这一年活跃,因此子集的规模有所不同.对于后续的网络分析,我需要一个加权有向的邻接矩阵,因此我使用[network package]的选项来创建它.我首先将其加载为网络对象,然后将其转换为邻接矩阵.
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")
结果矩阵看起来像
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.
我的想法是ATM:创建初始数据集和简化数据集的矩阵.然后,我将所有矩阵值都设置为零.然后我以某种方式将其与简化后的矩阵匹配,并在正确的行和列中使用正确的值填充它.不幸的是,我不知道这怎么可能.
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?
推荐答案
很遗憾,您的问题不清楚,所以我将尽力回答.
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?****
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
这篇关于R-匹配长度不同的矩阵的行和列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!