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

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

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:

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

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

combined_adj <- matrix(0,3,4)
colnames(combined_adj) <- c("g1","g2","g3","g4")
rownames(combined_adj) <- c("Tim","John", "Sarah")
combined_adj[1,4] <- 1
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的维度和行/列名称的零矩阵。
matreplace <- function(x, y, z){
  if(missing(z)) {
    z <- x
    z[] <- 0
  }
  ij <- Map(intersect, dimnames(z), dimnames(y))
  i <- ij[[1L]]
  j <- ij[[2L]]
  z[i, j] <- y[i, j]
  z
}

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

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

combined_adj <- matreplace(adj1, adj2)

adj2[2, 1] <- pi
matreplace(adj1, adj2, combined_adj)
#>             g1 g2 g3 g4
#> Tim   0.000000  0  0  1
#> John  3.141593  0  0  0
#> 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.
matreplace &lt;- function(x, y, z){
  if(missing(z)) {
    z &lt;- x
    z[] &lt;- 0
  }
  ij &lt;- Map(intersect, dimnames(z), dimnames(y))
  i &lt;- ij[[1L]]
  j &lt;- ij[[2L]]
  z[i, j] &lt;- y[i, j]
  z
}

adj1 &lt;- matrix(0, 3, 4)
colnames(adj1) &lt;- c(&quot;g1&quot;, &quot;g2&quot;, &quot;g3&quot;, &quot;g4&quot;)
rownames(adj1) &lt;- c(&quot;Tim&quot;, &quot;John&quot;, &quot;Sarah&quot;)

adj2 &lt;- matrix(0, 2, 2)
colnames(adj2) &lt;- c(&quot;g1&quot;, &quot;g4&quot;)
rownames(adj2) &lt;- c(&quot;Tim&quot;, &quot;John&quot;)
adj2[1,2] &lt;- 1

combined_adj &lt;- matreplace(adj1, adj2)

adj2[2, 1] &lt;- pi
matreplace(adj1, adj2, combined_adj)
#&gt;             g1 g2 g3 g4
#&gt; Tim   0.000000  0  0  1
#&gt; John  3.141593  0  0  0
#&gt; Sarah 0.000000  0  0  0

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

答案2

得分: 2

以下是您要翻译的内容:

这里有另外两种替代方法:

# 创建空矩阵
adj1 <- matrix(0, 3, 4)
colnames(adj1) <- c("g1", "g2", "g3", "g4")
rownames(adj1) <- c("Tim", "John", "Sarah")

# 创建子集矩阵
adj2 <- matrix(0, 2, 2)
colnames(adj2) <- c("g1", "g4")
rownames(adj2) <- c("Tim", "John")

# 更改和测试
adj2[1,2] <- 1
adj2[2, 1] <- pi

for 循环

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

match()

# 解决方案 2 使用 `match()`
row_idx <- match(rownames(adj2), rownames(adj1))
col_idx <- match(colnames(adj2), colnames(adj1))

adj1[row_idx, col_idx] <- adj2
adj1

输出

            g1       g2 g3       g4
Tim   0.000000 0.000000  0 1.000000
John  3.141593 0.000000  0 0.000000
Sarah 0.000000 0.000000  0 0.000000

<details>
<summary>英文:</summary>

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

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

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

### output
        g1 g2 g3 g4

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


</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:

确定