Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pivot longer with blocks of variables

I'm having trouble using the pivot_longer on blocks of variables. Suppose I have this:

enter image description here


and I want this:

enter image description here

dfwide <- structure(list(date = structure(c(1577836800, 1577923200, 1578009600, 
1578096000, 1578182400, 1578268800), class = c("POSIXct", "POSIXt"
), tzone = "UTC"), x1_a = c(20, 15, 12, NA, 25, 27), x1_b = c(33, 
44, 85, 10, 12, 3), x1_c = c(70, 20, 87, 11, 20, 5), x2_a = c(85, 
65, 33, 46, 82, 9), x2_b = c(87, 25, 55, 64, 98, 5), x2_c = c(77, 
51, 92, 20, 37, 98)), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame")) 

##Tried:
dfwide %>% 
  pivot_longer(cols = -date,
             names_sep = c("x1", "x2"),
             names_to = c("a", "b", "c"),
             values_to = "value")
like image 656
Thomas Speidel Avatar asked Oct 17 '25 19:10

Thomas Speidel


2 Answers

This line is taking advantage of the name separation option of the pivot_longer function.

pivot_longer(dfwide, -date, names_sep = "_", 
             names_to=c("which", ".value")) %>% 
   arrange(which)


    # A tibble: 12 x 5
   date                which     a     b     c
   <dttm>              <chr> <dbl> <dbl> <dbl>
 1 2020-01-01 00:00:00 x1       20    33    70
 2 2020-01-02 00:00:00 x1       15    44    20
 3 2020-01-03 00:00:00 x1       12    85    87
 4 2020-01-04 00:00:00 x1       NA    10    11
 5 2020-01-05 00:00:00 x1       25    12    20
 6 2020-01-06 00:00:00 x1       27     3     5
 7 2020-01-01 00:00:00 x2       85    87    77
 8 2020-01-02 00:00:00 x2       65    25    51
 9 2020-01-03 00:00:00 x2       33    55    92
10 2020-01-04 00:00:00 x2       46    64    20
11 2020-01-05 00:00:00 x2       82    98    37
12 2020-01-06 00:00:00 x2        9     5    98
like image 164
Dave2e Avatar answered Oct 20 '25 08:10

Dave2e


You can try this code:

library(tidyverse)
dfwide %>% 
  pivot_longer(cols = -date,
               names_to = "which",
               values_to = "value") %>%
  separate(which, into = c("which","letter"), sep = "_") %>%
  pivot_wider(names_from = "letter", values_from = "value") %>%
  arrange(which)

This is the result:

# A tibble: 12 x 5
   date                which     a     b     c
   <dttm>              <chr> <dbl> <dbl> <dbl>
 1 2020-01-01 00:00:00 x1       20    33    70
 2 2020-01-02 00:00:00 x1       15    44    20
 3 2020-01-03 00:00:00 x1       12    85    87
 4 2020-01-04 00:00:00 x1       NA    10    11
 5 2020-01-05 00:00:00 x1       25    12    20
 6 2020-01-06 00:00:00 x1       27     3     5
 7 2020-01-01 00:00:00 x2       85    87    77
 8 2020-01-02 00:00:00 x2       65    25    51
 9 2020-01-03 00:00:00 x2       33    55    92
10 2020-01-04 00:00:00 x2       46    64    20
11 2020-01-05 00:00:00 x2       82    98    37
12 2020-01-06 00:00:00 x2        9     5    98
like image 20
Manu Avatar answered Oct 20 '25 10:10

Manu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!