【问题标题】:Transform data frame some columns into rows [duplicate]将数据框的某些列转换为行[重复]
【发布时间】:2017-10-05 14:27:29
【问题描述】:

数据

我有一个这样的数据框:

 id weigth temp_s1 temp_s2
  1     50       2       7
  2     51       3       8
  3     52       4       9
  4     53       5      10
  5     54       6      11

我想要什么

我想得到这个:

     id weigth  temp    value
      1     50  temp_s1     2
      1     50  temp_s2     7
      1     51  temp_s1     3
      1     51  temp_s2     8
      1     52  temp_s1     4
      1     52  temp_s2     9
      1     53  temp_s1     5
      1     53  temp_s2     10
      1     54  temp_s1     6
      1     54  temp_s2     11

【问题讨论】:

  • 你能把 dput 的结果贴在你的 df 上吗?
  • 无法检查没有数据可以试穿但dat %>% tidyr::gather(temp, value, -weight)?

标签: r dataframe tidyr reshape2 tidyverse


【解决方案1】:

您应该使用reshape2 包中的melt 函数,如下所示:

melt(df, c("id", "weight"))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-16
    • 1970-01-01
    • 2023-01-14
    • 1970-01-01
    相关资源
    最近更新 更多