【问题标题】:Converting Elements in a Dataframe to Columns in a Table将数据框中的元素转换为表中的列
【发布时间】:2013-05-07 21:57:26
【问题描述】:

我有一个如下所示的数据框:

$RandArcBo1
$RandArcBo1$x
 [1]  150.5  166.5  135.5  177.5 -146.5  153.5 -152.5  152.5  157.5 -174.5 -155.5 -139.5 -129.5  136.5 -157.5   73.5

$RandArcBo1$y
 [1] 53.56732 78.56732 79.56732 58.56732 73.56732 57.56732 74.56732 55.56732 50.56732 84.56732 82.56732 81.56732 83.56732 78.56732
[15] 73.56732 73.56732

$RandArcBo1$Species
[1] "RandArcBo1"

我想把dataframe转成table,其中每个dataframe元素对应一个table中的一列,如下:

Species       x             y
RandArcBo1    150.5         53.56732
RandArcBo1    166.5         78.56732

..等等

我尝试过使用 xtabs(),但似乎无法以不会导致错误的方式来表达代码,并且还在研究 reshape 包,但到目前为止我一直受到阻碍。有什么想法吗?

【问题讨论】:

  • 这看起来像一个列表,而不是一个数据框。您能否提供有关对象的dput() 的输出?
  • 你试过as.data.frame(.)吗?
  • 您有一个包含列表的列表。只需as.data.frame(dat$RandArcBo1)

标签: r dataframe type-conversion


【解决方案1】:

使用data.table 包,它会做你想要的回收:

l = list(x = c(1:10), y = c(11:20), species = "test")

library(data.table)
as.data.table(l)
#     x  y species
# 1:  1 11    test
# 2:  2 12    test
# 3:  3 13    test
# 4:  4 14    test
# 5:  5 15    test
# 6:  6 16    test
# 7:  7 17    test
# 8:  8 18    test
# 9:  9 19    test
#10: 10 20    test

【讨论】:

    猜你喜欢
    • 2015-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-08
    • 2013-03-25
    • 2013-09-30
    • 2014-08-02
    • 1970-01-01
    相关资源
    最近更新 更多