Home > Net >  Pivoting longer for sequential data with various datatypes in R using pivot_longer
Pivoting longer for sequential data with various datatypes in R using pivot_longer

Time:10-04

To create graphs from my data that is currently in the wide format, I am trying to pivot this data to a longer format. Currently the data is like this:

type parameter_d1 parameter_d2
0 4 8
1 6 5

In reality there is many different parameters, but they all have the suffix _d1 and _d2.

Finally I would like to have a dataframe like the following:

type day parameter
0 1 4
0 2 8
1 1 6
1 2 5

I think this shouldn't be too hard to achieve but I can't find an example doing this anywhere.

CodePudding user response:

You can use matching groups and regular expression in the names_pattern argument in pivot_longer to do so. See the example below that I adjusted from a previous answer, where the parameters are y or se, and days are numbers:

library("tidyverse")

# Some data
raw_data <- data.frame(x = seq(10),
                       y_d1 = sample(x=20,size=10), # c(1.1, 2.4, 3.5, 4.1, 5.9, 6.7, 7.1, 8.3, 9.4, 10.0)
                       y_d2 = sample(x=20,size=10),
                       y_d3 = sample(x=20,size=10),
                       y_d4 = sample(x=20,size=10),
                       se_d1 = runif(n=10,min=0,max=1),
                       se_d2 = runif(n=10,min=0,max=1),
                       se_d3 = runif(n=10,min=0,max=1),
                       se_d4 = runif(n=10,min=0,max=1))

# Convert to a long format
raw_data %>%
  pivot_longer(!x,
               names_pattern = "([[:alpha:]] )_d([0-9])",
               names_to = c("parameter", "day"))
#> # A tibble: 80 x 4
#>        x parameter day    value
#>    <int> <chr>     <chr>  <dbl>
#>  1     1 y         1     12    
#>  2     1 y         2      1    
#>  3     1 y         3     12    
#>  4     1 y         4      9    
#>  5     1 se        1      0.840
#>  6     1 se        2      0.650
#>  7     1 se        3      0.523
#>  8     1 se        4      0.211
#>  9     2 y         1     15    
#> 10     2 y         2      6    
#> # ... with 70 more rows

Created on 2022-10-03 by the reprex package (v2.0.1)

CodePudding user response:

Here's a way with pivot_longer:

library(tidyr)
df %>% 
  pivot_longer(-type, names_sep = "_", names_to = c(".value", "day"), 
               names_transform = list(day = readr::parse_number))

   type   day parameter
1     0     1         4
2     0     2         8
3     1     1         6
4     1     2         5
  • Related