在列标题和成对变量中嵌入数据的数据透视表 [英] Pivot table with embedded data in column headings and paired variables

查看:21
本文介绍了在列标题和成对变量中嵌入数据的数据透视表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这样的数据,不幸的是在列名中嵌入了变量值:

Say I had data like this with variable values unfortunately embedded in the column names:

library(tidyr)
library(dplyr)
dat <- tribble(
  ~group, ~var1, ~var_meta1, ~var2, ~var_meta2,
  "group1", 5.2, "cat", 4.3, "dog"
)
dat
#> # A tibble: 1 x 5
#>   group   var1 var_meta1  var2 var_meta2
#>   <chr>  <dbl> <chr>     <dbl> <chr>    
#> 1 group1   5.2 cat         4.3 dog

我想将该数据转入其中,以便将列名中的数字作为变量.同时我想保持配对变量(var*var_meta*)之间的关系.

I'd like to pivot that data into this so that the number in the column name because a variable. At the same time I'd like to maintain the relationship between the paired variables (var* and var_meta*) .

#> # A tibble: 2 x 4
#>   group    day   var var_meta
#>   <chr>  <dbl> <dbl> <chr>   
#> 1 group1     1   5.2 cat     
#> 2 group1     2   4.3 dog

对于这种特殊情况,我真的只对 tidyr 解决方案感兴趣.我试过这个:

For this particular case I am really interested in a tidyr solution only. I've tried this:

dat %>%
  pivot_longer(-group,
    names_to = c("day", "var", ".value"),
    names_pattern = "([A-Za-z]+)_([A-Za-z]+)([0-9]+)"
  )
#> # A tibble: 3 x 5
#>   group  day   var   `1`   `2`  
#>   <chr>  <chr> <chr> <chr> <chr>
#> 1 group1 <NA>  <NA>  <NA>  <NA> 
#> 2 group1 var   meta  cat   dog  
#> 3 group1 <NA>  <NA>  <NA>  <NA>

还有这个:

dat %>%
  mutate(across(contains("var"), as.character)) %>%
  pivot_longer(-group,
    names_to = c("type", ".value"),
    names_pattern = "([A-Za-z]+)([0-9]+)"
  )
#> # A tibble: 2 x 4
#>   group  type  `1`   `2`  
#>   <chr>  <chr> <chr> <chr>
#> 1 group1 var   5.2   4.3  
#> 2 group1 meta  cat   dog

我觉得我很接近,但我无法完全理解正则表达式(我认为).

I feel like I am close but I can't quite wrap my head around the regex (I think).

有什么想法吗?

推荐答案

一个选项可能是:

dat %>%
 pivot_longer(-group,
              names_to = c(".value", "day"),
              names_pattern = "(\\D+)(\\d+)")

  group  day     var var_meta
  <chr>  <chr> <dbl> <chr>   
1 group1 1       5.2 cat     
2 group1 2       4.3 dog

这篇关于在列标题和成对变量中嵌入数据的数据透视表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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