【问题标题】:How to convert matrix to a data frame with the column and row names from the original data frame如何使用原始数据框中的列名和行名将矩阵转换为数据框
【发布时间】:2019-04-16 06:19:06
【问题描述】:

我有以下原始数据框:

original_df <- structure(c(0, 0, 0, 0, 1, 0, 0, 0, 0), .Dim = c(3L, 3L), .Dimnames = list(
  c("foo", "bar", "qux"), c("A", "B", "C")
))

original_df
#>     A B C
#> foo 0 0 0
#> bar 0 1 0
#> qux 0 0 0

然后我做了一些转换,得到一个普通的矩阵:

transformed_mat <- structure(c(
  -2.96100772320745e-06, 1.68169240440672e-05, -0.000126831814542474,
  -9.94017331567414e-07, 0.000763027661834236, -0.000103315552273569,
  -2.22776698138103e-06, 2.94317362067914e-05, -0.000190660599719715
), .Dim = c(3L, 3L))

transformed_mat
#>               [,1]          [,2]          [,3]
#> [1,] -2.961008e-06 -9.940173e-07 -2.227767e-06
#> [2,]  1.681692e-05  7.630277e-04  2.943174e-05
#> [3,] -1.268318e-04 -1.033156e-04 -1.906606e-04

如何使用original data frame 中的列名和行名屏蔽转换后的矩阵?

想要的结果是:

                A            B              C
foo -2.961008e-06 -9.940173e-07 -2.227767e-06
bar  1.681692e-05  7.630277e-04  2.943174e-05
qux -1.268318e-04 -1.033156e-04 -1.906606e-04

【问题讨论】:

    标签: r dataframe matrix


    【解决方案1】:

    我们可以使用dimnames 赋值,因为它们都是matrixes

    dimnames(transformed_mat) <- dimnames(original_df)
    transformed_mat
    #                A             B             C
    #foo -2.961008e-06 -9.940173e-07 -2.227767e-06
    #bar  1.681692e-05  7.630277e-04  2.943174e-05
    #qux -1.268318e-04 -1.033156e-04 -1.906606e-04
    

    由于dimnames是一个属性,另一种方式是attr通过赋值

    attr(transformed_mat, "dimnames") <- attr(original_df, "dimnames")
    

    【讨论】:

      【解决方案2】:

      只需使用:dimnames(transformed_mat)

      请参阅下面的工作示例:

      > transformed_mat
                  A             B             C
      foo -2.961008e-06 -9.940173e-07 -2.227767e-06
      bar  1.681692e-05  7.630277e-04  2.943174e-05
      qux -1.268318e-04 -1.033156e-04 -1.906606e-04
      >
      >
      >
      > original_df <- structure(c(0, 0, 0, 0, 1, 0, 0, 0, 0), .Dim = c(3L, 3L), .Dimnames = list(
      +     c("foo", "bar", "qux"), c("A", "B", "C")
      + ))
      > original_df
      A B C
      foo 0 0 0
      bar 0 1 0
      qux 0 0 0
      > transformed_mat <- structure(c(
      +     -2.96100772320745e-06, 1.68169240440672e-05, -0.000126831814542474,
      +     -9.94017331567414e-07, 0.000763027661834236, -0.000103315552273569,
      +     -2.22776698138103e-06, 2.94317362067914e-05, -0.000190660599719715
      + ), .Dim = c(3L, 3L))
      > transformed_mat
                    [,1]          [,2]          [,3]
      [1,] -2.961008e-06 -9.940173e-07 -2.227767e-06
      [2,]  1.681692e-05  7.630277e-04  2.943174e-05
      [3,] -1.268318e-04 -1.033156e-04 -1.906606e-04
      > dimnames(transformed_mat)<-dimnames(original_df)
      > transformed_mat
                      A             B             C
      foo -2.961008e-06 -9.940173e-07 -2.227767e-06
      bar  1.681692e-05  7.630277e-04  2.943174e-05
      qux -1.268318e-04 -1.033156e-04 -1.906606e-04
      

      【讨论】:

      • 是的,先生,它是一样的。我认为它在草稿中的时间比平时更长。
      • 好的,没问题。我想我们可以责怪交通:=)
      猜你喜欢
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 2018-07-15
      • 2014-10-30
      • 1970-01-01
      • 1970-01-01
      • 2017-10-28
      • 1970-01-01
      相关资源
      最近更新 更多