【问题标题】:Issues reshaping data.frame from wide to long format从宽格式到长格式重塑 data.frame 的问题
【发布时间】:2020-01-13 13:41:45
【问题描述】:

我正在尝试将以下数据框重塑为长格式,但使用 reshape() 或 melt() 编写代码时运气不佳。

                 pH                         SO4     
Lake  Lat   Long  1976   1977   1978  1981  1976  1977
1     58    7.2   4.59   -9999  4.48  4.63  6.5-  -9999
2     58.1  6.3   4.97   -9999  4.6   4.96  5.5-  -9999
4     58.5  7.9   4.32   4.23   4.4   4.49  4.8   6.5
5     58.6  8.9   4.97   4.74   4.98  5.21  7.4   7.6

我需要创建一个数据框,其中每一行定义一个唯一的 湖泊和年份的组合,每个化学变量的列,以及带有测量年份的附加列。

【问题讨论】:

标签: r reshape melt


【解决方案1】:

melt()cast() 太旧了。

最近,引入了函数 pivot_long()pivot_wide()。 (这个函数替换了 gather()spread() 替换了 melt()cast()

看看this

【讨论】:

    猜你喜欢
    • 2021-10-26
    相关资源
    最近更新 更多