【发布时间】:2021-05-01 16:08:14
【问题描述】:
我有一个格式如下的 df:
| name | other_info | revenues_2015 | ebitda_2015 | ebitda_2016 | revenues_2015 | other_2017 |
|---|---|---|---|---|---|---|
| A | Info1 | 1 | 2 | 3 | 4 | 5 |
| B | Info2 | 6 | 7 | 8 | 9 | 10 |
| C | Info3 | 11 | 12 | 13 | 14 | 15 |
我想将其更改为长格式,并按以下方式构建:
姓名 |信息 |年份 |指标名称 |价值
你能告诉我如何在 R 中做到这一点吗? 由于真实数据框有300多列,有没有办法自动创建年份列?
数据:
structure(list(name = structure(1:3, .Label = c("A", "B", "C"
), class = "factor"), other_info = structure(1:3, .Label = c("Info1",
"Info2", "Info3"), class = "factor"), revenues_2015 = structure(c(1L,
3L, 2L), .Label = c("1", "11", "6"), class = "factor"), ebitda_2015 = structure(c(2L,
3L, 1L), .Label = c("12", "2", "7"), class = "factor"), ebitda_2016 = structure(c(2L,
3L, 1L), .Label = c("13", "3", "8"), class = "factor"), revenues_2015 = structure(c(2L,
3L, 1L), .Label = c("14", "4", "9"), class = "factor"), other_2017 = structure(c(3L,
1L, 2L), .Label = c("10", "15", "5"), class = "factor")), class = "data.frame", row.names = c(NA,
-3L))
【问题讨论】:
-
是的。您可以使用 tidyverse 包中的 pivot_longer 来做到这一点。有一个名为 names_sep 的参数,您可以在其中指定在下划线处拆分名称。