繁体   English   中英

将宽 dataframe 重塑为长格式

[英]Reshaping wide dataframe to long format

我有以下格式的df:

姓名 其他信息 收入_2015 ebitda_2015 ebitda_2016 收入_2015 其他_2017
一个 信息1 1 2 3 4 5
信息2 6 7 8 9 10
C 信息3 11 12 13 14 15

我想将其更改为长格式,并按以下方式构建:

姓名 | 信息 | 年份 | 指标名称 | 价值

你能告诉我如何在 R 中做到这一点吗? 既然真正的dataframe有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))

这对你有用吗?

library(dplyr)
library(tidyr)

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)) %>% 
  pivot_longer(revenues_2015:other_2017, names_pattern = "(.+)_(\\d{4})", names_to = c("metric", "year"))

您有两个选择,您可以使用实用工具 package(base-r 函数,您不必使用 library() 调用它)或从 reshape2 ZEFE90A8E604A7C840E8ZD03A 熔化 function

使用function reshape()

 data = 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))

LF_data = reshape(data=data, idvar = c("name","other_info"), varying =c("revenues_2015","ebitda_2015","ebitda_2016","revenues_2015","other_2017"), 
    v.names = c("Value"),times=c("revenues_2015","ebitda_2015","ebitda_2016","revenues_2015","other_2017"), direction = "long")

使用package reshape2 melt() function:

  1. 首先,您需要具有属性 stringsAsFactor = False 的 dataframe
       data=data.frame(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)),stringsAsFactors=False)

 2. Then:
LF_data=reshape2::melt(data,id.vars=c("name","other_info"), mesure.vars=c("revenues_2015","ebitda_2015","ebitda_2016","revenues_2015","other_2017"))

除非它们是唯一的,否则融化不会让您拥有“名称”、“其他信息”和“变量”的组合。 在您的示例中,它将第二个三元组的收入_2015 更改为收入_2015.1

有点太晚了:类似于-mad-statter 解决方案。 使用 mutate 略有不同:

library(tidyr)
library(dplyr)

df <- 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)) %>% 
  pivot_longer(revenues_2015:other_2017, names_to = c("Metric name", "Year"),
               names_sep ="_", values_to = "Value") %>% 
  dplyr::mutate(Year = stringr::str_remove(Year, "\\D")) %>% 
  rename(Name=name, Info = other_info)

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM