【问题标题】:Reorganisation of data frame in rr中数据帧的重组
【发布时间】:2015-04-23 21:29:49
【问题描述】:

我有这个数据框:

> data
        Date Treatment Plate.1 Plate.2 Plate.3 Plate.4 Plate.5 Plate.6 Plate.7 Plate.8 
1 2014-10-15         2       1       0       2       5       2       0      11       6 
2 2014-11-12         2       3       2       3       0       0       3       2       1
3 2014-12-11         2       1       0       1       1       0       1       1       0 
4 2015-02-11         2       0       0       0       0       0       0       0       0 
5 2015-03-09         2       0       0       0       0       0       0       0       0

我一直在尝试将其重新组织成这种格式:

> manually.created.dataframe
        Date Treatment Abundance
1 2014-10-15         2       1       
  2014-10-15         2       0
  2014-10-15         2       2
  2014-10-15         2       5
  2014-10-15         2       2
  2014-10-15         2       0
  2014-10-15         2       11
  2014-10-15         2       3
  2014-11-12         2       2
  2014-11-12         2       3
  2014-11-12         2       0
etc.

我尝试实现转置功能无济于事。任何帮助或建议将不胜感激。谢谢!

编辑:对不起,我应该更清楚。丰度是来自 Plate.1,Plate 的值。 2、盘子。每个日期值 3 个等。我想将板 1-8 放入标题为“丰富”的垂直列中,复制相邻垂直列中的处理值和日期。

【问题讨论】:

  • 在这种情况下应该衡量什么丰度?似乎 reshape2 包中的功能 melt 可能是您想要的,但我不确定您在第三列中想要什么。
  • 这是一种相当混乱的数据格式。您需要另一列指定车牌号。

标签: r dataframe transpose


【解决方案1】:

您可以使用 reshape2 包中的 melt 函数:

melt(data, id.vars = c("Date", "Treatment"), value.name = "Abundance")

您将在输出数据集中获得一个名为 variable 的附加列,其中包含板名称

【讨论】:

    【解决方案2】:

    试试这个:

    library(reshape2)
    x = melt(data,id = c("Date","Treatment"))
    

    这包含的列比您想要的解决方案中的多,但我认为您可能还是想要这些列,因为这会告诉您值来自哪个板块。

    但如果不是你可以这样做:

    x = x[-3]
    

    【讨论】:

      猜你喜欢
      • 2011-12-22
      • 1970-01-01
      • 2018-12-14
      • 1970-01-01
      • 2017-08-10
      • 2018-12-28
      • 2023-03-19
      • 2020-04-26
      • 1970-01-01
      相关资源
      最近更新 更多