简体   繁体   English

如何通过行/列名将矩阵单元格值相加到另一个?

[英]How to sum a matrix cell value into another by row/column names?

Imagine I have an overall list of authors想象一下我有一个完整的作者列表

Authors <- c("Abel","Babel","Cain","Devil","Esau")

with it I build an overall adjacency matrix, initialized with zeroes用它我建立了一个整体邻接矩阵,用零初始化

allAuthors <- matrix(0L,nrow=length(Authors),ncol=length(Authors),dimnames=list(Authors,Authors))

now I am stumbling on a paper coAuthored by these three guys现在我偶然发现了这三个人合着的一篇论文

paperAuthors <- c("Babel","Cain","Devil")

and build another adjacency matrix of their collaboration, initialized with all 1s并构建他们协作的另一个邻接矩阵,初始化为全 1

coAuth <- matrix(1L,nrow=length(paperAuthors),ncol=length(paperAuthors),dimnames=list(paperAuthors,paperAuthors))

Question :问题 :

How do I sum the coAuth matrix cell values into the corresponding allAuthors matrix cells using the row and colum names as indices ?如何使用行和列名称作为索引将 coAuth 矩阵单元格值加到相应的 allAuthors 矩阵单元格中?

In other words I'd like to obtain the cells of the allAuthors matrix having 1s at the intersection of the paperAuthors authors while all other remain 0s.换句话说,我想在paperAuthors 作者的交点处获得allAuthors 矩阵的单元格,而所有其他矩阵的单元格都为0。

Thank you very much非常感谢

Would subsetting work for your needs or do you need to define new matrix summation operation?子集化是否适合您的需求,或者您是否需要定义新的矩阵求和运算?

allAuthors[paperAuthors, paperAuthors] <- 
  allAuthors[paperAuthors, paperAuthors] + 1
allAuthors

First we get the indexes in the coAuth matrix.首先我们得到coAuth矩阵中的索引。

ind <- which(coAuth == 1, arr.ind = TRUE)

Now we have to find the corresponding indexes in the allAuthors matrix.现在我们必须在allAuthors矩阵中找到相应的索引。

ind.allAuthors <- cbind(
    match(rownames(coAuth), rownames(allAuthors))[ind[, 'row']],
    match(colnames(coAuth), colnames(allAuthors))[ind[, 'col']])

And now we can sum the elements from both matrices:现在我们可以将两个矩阵的元素相加:

allAuthors[ind.allAuthors] <- allAuthors[ind.allAuthors] + 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何按列名和行名对单元格的值求和以在 R 中创建矩阵? - How to sum values of cells by their column and row names to create a matrix in R? 根据R中同一行的另一列中的单元格的值对一列求和 - Sum a column based on the value of a cell in another column of the same row in R 如何对矩阵中的列求和并将值粘贴到 r 的最后一个单元格中? - How to sum a column in a matrix and paste the value in the last cell in r? 如何使用单元格名称的行和列名称获取匹配R中条件的矩阵(例如,相关矩阵)中的单元格名称? - How to get names of cells in matrix (e.g., correlation matrix) which match a condition in R using row and column names for cell names? 如何对 R 中的一列求和,该列被另一列的值过滤? 还是到特定的行? - How to sum a column in R filtered by the value of another column? Or to a specific row? 如何基于列/行名称将矩阵中的值替换为另一个矩阵? - How to replace values from a matrix with another matrix based on column/row names? 矩阵的列名和行名? - column and row names for a matrix? 如何基于行和列名称合并矩阵 - How to merge matrix based on row and column names 使用矩阵中的值提取行名和列名 - Extracting row and column names with its value from matrix 提取矩阵中每一行的最大值并输出其列名 - Extracting max value of each row in a matrix and output their column names
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM