从R中的数据框创建对角矩阵

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

Create a diagonal matrix from a dataframe in r

问题

我有这个数据框:

  1. data.frame(Name = c("A", "B"),
  2. Value = c(1,2))

我想得到一个矩阵,其中Name值作为矩阵的行名和列名,值作为以下减法乘积的结果:

从R中的数据框创建对角矩阵

又被称为

从R中的数据框创建对角矩阵

英文:

I have this data frame:

从R中的数据框创建对角矩阵

  1. data.frame(Name = c("A", "B"),
  2. Value = c(1,2))

and I would like to arrive at a matrix with Name values as the matrix's row names and column names and values as the product of a subtraction like the following

从R中的数据框创建对角矩阵

AKA

从R中的数据框创建对角矩阵

答案1

得分: 3

我假设您想要使用矩阵运算来完成,所以这是我的努力。

  1. dd <- data.frame(Name = c("A", "B"),
  2. Value = c(1,2))
  3. M <- matrix( dd$Value, 2,2, dimnames=list( dd$Name, dd$Name))
  4. M
  5. # A B
  6. #A 1 1
  7. #B 2 2
  8. M - t(M)
  9. # A B
  10. #A 0 -1
  11. #B 1 0

如果您需要进一步的帮助,请告诉我。

英文:

I assumed you wanted it done with matrix operations, so here's my effort.

  1. dd &lt;- data.frame(Name = c(&quot;A&quot;, &quot;B&quot;),
  2. Value = c(1,2))
  3. M &lt;- matrix( dd$Value, 2,2, dimnames=list( dd$Name, dd$Name))
  4. M
  5. # A B
  6. #A 1 1
  7. $B 2 2
  8. M - t(M)
  9. # A B
  10. #A 0 -1
  11. #B 1 0

答案2

得分: 2

你可以使用函数 outer -

  1. df <- data.frame(Name = c("A", "B"), Value = c(1,2))
  2. mat <- t(outer(df$Value, df$Value, `-`))
  3. dimnames(mat) <- list(df$Name, df$Name)
  4. mat
  5. # A B
  6. #A 0 1
  7. #B -1 0
英文:

You can use the function outer -

  1. df &lt;- data.frame(Name = c(&quot;A&quot;, &quot;B&quot;), Value = c(1,2))
  2. mat &lt;- t(outer(df$Value, df$Value, `-`))
  3. dimnames(mat) &lt;- list(df$Name, df$Name)
  4. mat
  5. # A B
  6. #A 0 1
  7. #B -1 0

答案3

得分: 1

A tidyverse solution:

  1. df <- data.frame(name = c("A", "B"), value = c(1,2))
  2. df |>
  3. bind_cols(df |&gt; pivot_wider()) |>
  4. transmute(across(A:B, \(x) x - value))
  5. A B
  6. 1 0 1
  7. 2 -1 0

*注意,此示例中的 namevalue 是小写。如果它们是大写的,或者存在其他列名,可以在 pivot_wider() 中使用 names_fromvalues_from 参数。
示例:pivot_wider(names_from = 'Name', values_from = "Value").

英文:

A tidyverse solution:

  1. df &lt;- data.frame(name = c(&quot;A&quot;, &quot;B&quot;), value = c(1,2))
  2. df |&gt;
  3. bind_cols(df |&gt; pivot_wider()) |&gt;
  4. transmute(across(A:B, \(x) x - value))
  5. A B
  6. 1 0 1
  7. 2 -1 0

*Note name and value are lowercase in this example. If they are capitalized or if there are other column names, use the names_from and values_from args in pivot_wider().
Example: pivot_wider(names_from = &#39;Name&#39;, values_from = &quot;Value&quot;).

答案4

得分: 1

使用 col() 进行索引的另一种方法:

  1. mat <- array(df$Value, rep(nrow(df), 2), dimnames = list(df$Name, df$Name))
  2. df$Value[col(mat)] - mat
  3. # A B
  4. # A 0 1
  5. # B -1 0

请注意,我已经删除了代码部分并提供了翻译的内容。

英文:

An alternative approach with col() for indexing:

  1. mat &lt;- array(df$Value, rep(nrow(df), 2), dimnames = list(df$Name, df$Name))
  2. df$Value[col(mat)] - mat
  3. # A B
  4. # A 0 1
  5. # B -1 0

答案5

得分: 1

这只是吗?

  1. col(df) - df$value
  2. [,1] [,2]
  3. [1,] 0 1
  4. [2,] -1 0
  5. with(df, structure(col(df) - value, .Dimnames = list(name, name)))
  6. A B
  7. A 0 1
  8. B -1 0
英文:

Isnt this just?

  1. col(df) - df$value
  2. [,1] [,2]
  3. [1,] 0 1
  4. [2,] -1 0
  5. with(df, structure(col(df) - value, .Dimnames = list(name, name)))
  6. A B
  7. A 0 1
  8. B -1 0

huangapple
  • 本文由 发表于 2023年3月1日 10:13:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75598981.html
匿名

发表评论

匿名网友

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

确定