如何pivot_longer一组多列?以及如何从这种长格式恢复到原始宽度? [英] How to pivot_longer a set of multiple columns? and How to go back from that long format to original wide?

查看:59
本文介绍了如何pivot_longer一组多列?以及如何从这种长格式恢复到原始宽度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有以下数据:

D = tibble::tribble(
  ~firm, ~ind, ~var1_1, ~var1_2, ~op2_1, ~op2_2,
  "A",     1,     10,     11,     11,     12,
  "A",     2,     12,     13,     13,     14,
  "B",     1,     14,     15,     15,     16,
  "B",     2,     16,     17,     17,     18,
  "C",     1,     18,     19,     19,     20,
  "C",     2,     20,     21,     21,     22,
)

我如何 pivot_longer() var1和var2以"_ *"作为年份指示符?

How can I pivot_longer() var1 and var2 having "_*" as year indicator?

我的意思是,我想要这样的东西:

I mean, I would like have something like this:

D %>%
  pivot_longer(var1_1:op2_2,
  names_to = c(".value", "year"),
  names_pattern = "(.*)_(.*)",
  values_to = c("var1, var2")
  )
# A tibble: 12 x 5
   firm    ind year   var1  op2
   <chr> <dbl> <chr> <dbl> <dbl>
 1 A         1 1        10    11
 2 A         1 2        11    12
 3 A         2 1        12    13
 4 A         2 2        13    14
 5 B         1 1        14    15
 6 B         1 2        15    16
 7 B         2 1        16    17
 8 B         2 2        17    18
 9 C         1 1        18    19
10 C         1 2        19    20
11 C         2 1        20    21
12 C         2 2        21    22

我正在使用上面的代码达到预期的结果.但是,在我的实际情况下,我要处理30多个变量和10年的时间.然后,使用 values_to 既不实用也不干净.我希望代码读取变量名的第一部分作为所需的新变量名.由于最初所有要透视的列的结构都类似于"varname_year" .

I'm achieving the desired result using the code above. However in my real case I'm dealing with more than 30 variables and 10 years. Then, using values_to isn't practical and clean. I'd like the code read first part of variable name as the desired new variable name. Since initially all columns to be pivoted are structured like "varname_year".

此外,一旦我将新数据格式弄长了,我可能需要回到宽格式以保持初始数据结构.

Besides, once I get the new data format into long, I might need to go back to wide-format keeping the initial data structure.

推荐答案

我们可以使用 select_helpers

library(dplyr)
library(tidyr)
library(stringr)
Dlong <- D %>%
          pivot_longer(cols = starts_with('var'), 
             names_to = c(".value", "year"), names_sep = "_")


从长"格式开始,使用 pivot_wider

Dlong %>%
    pivot_wider(names_from = ind, values_from = str_c("var", 1:2))

这篇关于如何pivot_longer一组多列?以及如何从这种长格式恢复到原始宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆