如何在将数据框转换为矩阵时取消列出?

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

How to unlist when converting dataframe to matrix?

问题

I want to coerce a numeric dataframe into matrix and want to retain the row/column names. I tried to unlist.

  1. mat <- as.matrix(unlist(df))

Input:

  1. > dput(df)
  2. structure(list(TCGA.2K.A9WE.01A = c("3.33370417925201", "3.26191613710211",
  3. "2.98072012253114", "3.45442444300391", "3.23147506581216"),
  4. TCGA.2Z.A9J1.01A = c("3.34925376425734", "3.32510595062856",
  5. "3.05313491041203", "3.63034209087263", "3.24014574406411" ),
  6. TCGA.2Z.A9J3.01A = c("3.30349695880794", "3.33666480834738",
  7. "3.08982082695446", "3.70172491168742", "3.27383206099273" ),
  8. TCGA.2Z.A9J5.01A = c("3.38096309720271", "3.38548662709543",
  9. "3.00519898142942", "3.57422385519763", "3.43484025303272" ),
  10. TCGA.2Z.A9J6.01A = c("3.36824732623076", "3.31393112898833",
  11. "3.28265423939019", "3.58687437256901", "3.33930301693651" )), row.names = c("AAAS", "AARSD1", "AASS", "ABHD11", "ABHD14A"), class = "data.frame")
  12. Data structure
  13. > class(df)
  14. [1] "data.frame"
  15. > typeof(df)
  16. [1] "list"

(Note: The provided text appears to be code and data. If you have any specific translation needs or questions, please let me know.)

英文:

I want to coerce a numeric dataframe into matrix and want to retain the row/column names. I tried to unlist.

  1. mat &lt;- as.matrix(unlist(df))

Input:

  1. &gt; dput(df)
  2. structure(list(TCGA.2K.A9WE.01A = c(&quot;3.33370417925201&quot;, &quot;3.26191613710211&quot;,
  3. &quot;2.98072012253114&quot;, &quot;3.45442444300391&quot;, &quot;3.23147506581216&quot;),
  4. TCGA.2Z.A9J1.01A = c(&quot;3.34925376425734&quot;, &quot;3.32510595062856&quot;,
  5. &quot;3.05313491041203&quot;, &quot;3.63034209087263&quot;, &quot;3.24014574406411&quot;
  6. ), TCGA.2Z.A9J3.01A = c(&quot;3.30349695880794&quot;, &quot;3.33666480834738&quot;,
  7. &quot;3.08982082695446&quot;, &quot;3.70172491168742&quot;, &quot;3.27383206099273&quot;
  8. ), TCGA.2Z.A9J5.01A = c(&quot;3.38096309720271&quot;, &quot;3.38548662709543&quot;,
  9. &quot;3.00519898142942&quot;, &quot;3.57422385519763&quot;, &quot;3.43484025303272&quot;
  10. ), TCGA.2Z.A9J6.01A = c(&quot;3.36824732623076&quot;, &quot;3.31393112898833&quot;,
  11. &quot;3.28265423939019&quot;, &quot;3.58687437256901&quot;, &quot;3.33930301693651&quot;
  12. )), row.names = c(&quot;AAAS&quot;, &quot;AARSD1&quot;, &quot;AASS&quot;, &quot;ABHD11&quot;, &quot;ABHD14A&quot;
  13. ), class = &quot;data.frame&quot;)

Data structure

  1. &gt; class(df)
  2. [1] &quot;data.frame&quot;
  3. &gt; typeof(df)
  4. [1] &quot;list&quot;

答案1

得分: 2

你可以直接使用as.matrix(),无需进行unlist()操作:

  1. mat <- as.matrix(df)
  2. mode(mat) <- "numeric"

如果你选择使用unlist(),似乎需要更多步骤:

  1. mat <- matrix(as.numeric(unlist(df)), ncol = ncol(df))
  2. dimnames(mat) <- list(row.names(df), names(df))
英文:

You can apply as.matrix() directly without unlist()'ing:

  1. mat &lt;- as.matrix(df)
  2. mode(mat) &lt;- &quot;numeric&quot;

If you rather unlist() it seems there will be more steps:

  1. mat &lt;- matrix(as.numeric(unlist(df)), ncol = ncol(df))
  2. dimnames(mat) &lt;- list(row.names(df), names(df))

答案2

得分: 0

你可以将每一列转换为数字,然后将其放入一个矩阵中,如下所示:

  1. as.matrix(sapply(df, as.numeric))
  2. # 添加行名
  3. row.names(mat) <- row.names(df)
英文:

You can covert each column to a numeric then put it into a matrix like so:

  1. as.matrix(sapply(df,as.numeric))
  2. #add row names
  3. row.names(mat) &lt;- row.names(df)

huangapple
  • 本文由 发表于 2023年5月25日 23:09:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76333813.html
匿名

发表评论

匿名网友

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

确定