Pivot table with embedded data in column headings and paired variables

Viewed 83

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

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

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>

and this:

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).

Any ideas?

3 Answers

One option could be:

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

We can use also use names_sep with a regex lookaround to split between a non-digit (\\D) and a digit (\\d)

library(dplyr)
library(tidyr)
dat %>%
  pivot_longer(-group,
            names_to = c(".value", "day"),
           names_sep = "(?<=\\D)(?=\\d)")

-output

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

A base R option using reshape

reshape(
  setNames(dat, gsub("(\\d+)$", ".\\1", names(dat))),
  direction = "long",
  idvar = "group",
  varying = -1,
  timevar = "day"
)
Related