基于确切的行/列名称插入矩阵数值。

huangapple go评论100阅读模式
英文:

Insert matrix values based on exact row/column names

问题

I want to insert the information from the cells in a given subsets into the blank matrix based on the combination of row/column name.

以下是要翻译的部分。

英文:

I have a blank matrix with row and column names that is relatively large (6000 times 200). Then, I have multiple matrices which represent subsets of this matrix (typically around 1500 times 120) and also contain row and column names. I want to insert the information from the cells in a given subsets into the blank matrix based on the combination of row/column name.

Here is an example of the data and the desired output:

  1. adj1 <- matrix(0, 3, 4)
  2. colnames(adj1) <- c("g1", "g2", "g3", "g4")
  3. rownames(adj1) <- c("Tim", "John", "Sarah")
  4. adj2 <- matrix(0, 2, 2)
  5. colnames(adj2) <- c("g1", "g4")
  6. rownames(adj2) <- c("Tim", "John")
  7. adj2[1,2] <- 1
  8. adj2
  9. combined_adj <- matrix(0,3,4)
  10. colnames(combined_adj) <- c("g1","g2","g3","g4")
  11. rownames(combined_adj) <- c("Tim","John", "Sarah")
  12. combined_adj[1,4] <- 1
  13. combined_adj

I have somewhat managed to intersect by column names, but not by the exact combination of row/col names.

答案1

得分: 2

以下是您要求的代码部分的翻译:

这是一个允许您按行和列名称替换值的函数。
它的参数包括:

  • x 较大的矩阵;
  • y 具有新值的矩阵;
  • z 目标矩阵; 如果缺少,则将创建一个具有x的维度和行/列名称的零矩阵。
  1. matreplace <- function(x, y, z){
  2. if(missing(z)) {
  3. z <- x
  4. z[] <- 0
  5. }
  6. ij <- Map(intersect, dimnames(z), dimnames(y))
  7. i <- ij[[1L]]
  8. j <- ij[[2L]]
  9. z[i, j] <- y[i, j]
  10. z
  11. }
  12. adj1 <- matrix(0, 3, 4)
  13. colnames(adj1) <- c("g1", "g2", "g3", "g4")
  14. rownames(adj1) <- c("Tim", "John", "Sarah")
  15. adj2 <- matrix(0, 2, 2)
  16. colnames(adj2) <- c("g1", "g4")
  17. rownames(adj2) <- c("Tim", "John")
  18. adj2[1,2] <- 1
  19. combined_adj <- matreplace(adj1, adj2)
  20. adj2[2, 1] <- pi
  21. matreplace(adj1, adj2, combined_adj)
  22. #> g1 g2 g3 g4
  23. #> Tim 0.000000 0 0 1
  24. #> John 3.141593 0 0 0
  25. #> Sarah 0.000000 0 0 0

在2023-05-07使用reprex v2.0.2创建

英文:

Here is a function that allows you to replace values by row and column names.
Its arguments are

  • x the larger matrix;
  • y the matrix with new values;
  • z the target matrix; if missing a new matrix of zeros will be created with the dimensions and row/col names of x.
  1. matreplace &lt;- function(x, y, z){
  2. if(missing(z)) {
  3. z &lt;- x
  4. z[] &lt;- 0
  5. }
  6. ij &lt;- Map(intersect, dimnames(z), dimnames(y))
  7. i &lt;- ij[[1L]]
  8. j &lt;- ij[[2L]]
  9. z[i, j] &lt;- y[i, j]
  10. z
  11. }
  12. adj1 &lt;- matrix(0, 3, 4)
  13. colnames(adj1) &lt;- c(&quot;g1&quot;, &quot;g2&quot;, &quot;g3&quot;, &quot;g4&quot;)
  14. rownames(adj1) &lt;- c(&quot;Tim&quot;, &quot;John&quot;, &quot;Sarah&quot;)
  15. adj2 &lt;- matrix(0, 2, 2)
  16. colnames(adj2) &lt;- c(&quot;g1&quot;, &quot;g4&quot;)
  17. rownames(adj2) &lt;- c(&quot;Tim&quot;, &quot;John&quot;)
  18. adj2[1,2] &lt;- 1
  19. combined_adj &lt;- matreplace(adj1, adj2)
  20. adj2[2, 1] &lt;- pi
  21. matreplace(adj1, adj2, combined_adj)
  22. #&gt; g1 g2 g3 g4
  23. #&gt; Tim 0.000000 0 0 1
  24. #&gt; John 3.141593 0 0 0
  25. #&gt; Sarah 0.000000 0 0 0

<sup>Created on 2023-05-07 with reprex v2.0.2</sup>

答案2

得分: 2

以下是您要翻译的内容:

  1. 这里有另外两种替代方法:
  2. # 创建空矩阵
  3. adj1 <- matrix(0, 3, 4)
  4. colnames(adj1) <- c("g1", "g2", "g3", "g4")
  5. rownames(adj1) <- c("Tim", "John", "Sarah")
  6. # 创建子集矩阵
  7. adj2 <- matrix(0, 2, 2)
  8. colnames(adj2) <- c("g1", "g4")
  9. rownames(adj2) <- c("Tim", "John")
  10. # 更改和测试
  11. adj2[1,2] <- 1
  12. adj2[2, 1] <- pi

for 循环

  1. # 解决方案 1 使用 for 循环
  2. # 根据行和列名称在空白矩阵中填入值
  3. for (i in rownames(adj2)) {
  4. for (j in colnames(adj2)) {
  5. adj1[i,j] <- adj2[i,j]
  6. }
  7. }
  8. adj1

match()

  1. # 解决方案 2 使用 `match()`
  2. row_idx <- match(rownames(adj2), rownames(adj1))
  3. col_idx <- match(colnames(adj2), colnames(adj1))
  4. adj1[row_idx, col_idx] <- adj2
  5. adj1

输出

  1. g1 g2 g3 g4
  2. Tim 0.000000 0.000000 0 1.000000
  3. John 3.141593 0.000000 0 0.000000
  4. Sarah 0.000000 0.000000 0 0.000000
  1. <details>
  2. <summary>英文:</summary>
  3. Here are two more alternatives:

Create blank matrix

adj1 <- matrix(0, 3, 4)
colnames(adj1) <- c("g1", "g2", "g3", "g4")
rownames(adj1) <- c("Tim", "John", "Sarah")

Create subset matrix

adj2 <- matrix(0, 2, 2)
colnames(adj2) <- c("g1", "g4")
rownames(adj2) <- c("Tim", "John")

change and test

adj2[1,2] <- 1
adj2[2, 1] <- pi

  1. ### for loop

solution 1 for loop

Fill in values in blank matrix based on row and column names

for (i in rownames(adj2)) {
for (j in colnames(adj2)) {
adj1[i,j] <- adj2[i,j]
}
}
adj1

  1. ### match()

solution 2 using match()

row_idx <- match(rownames(adj2), rownames(adj1))
col_idx <- match(colnames(adj2), colnames(adj1))

adj1[row_idx, col_idx] <- adj2
adj1

  1. ### output
  1. g1 g2 g3 g4

Tim 0.000000 0 0 1
John 3.141593 0 0 0
Sarah 0.000000 0 0 0

  1. </details>

huangapple
  • 本文由 发表于 2023年5月7日 20:45:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76194020.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定