Home > Net >  Is there a way to use pivot_longer with an output of a shared column
Is there a way to use pivot_longer with an output of a shared column

Time:04-08

I'm trying to use pivot_longer with the repeated measures data. I'm struggling because they have a shared variable.

Basically, my data includes target stimuli and their corresponding responses, also an intervention condition with pre-post levels. There are 4 different targets but they were presented in mixed order. So the pre-post order of the targets is not identical.

SubjNo Group   Target_Pre Response_Pre Target_Post Response_Post  
  122   Gr1       3             2,5        4          3,3
  122   Gr1       2             1,8        1           2
  122   Gr1       1             1,7        3          1,9
  122   Gr1       4             3,5        2          2,3
  123   Gr2       4             2,6        1           2
  123   Gr2       1             1,4        3          1,5
  123   Gr2       3             2,6        2          2,3
  123   Gr2       2             1,5        4           3

The outcome I want is;

SubjNo Group   Target  Response  Condition
 122   Gr1       3      2,5       Pre
 122   Gr1       2      1,8       Pre
 122   Gr1       1      1,7       Pre
 122   Gr1       4      3,5       Pre
 123   Gr2       4      2,6       Pre
 123   Gr2       1      1,4       Pre 
 123   Gr2       3      2,6       Pre
 123   Gr2       2      1,5       Pre
 122   Gr1       4      3,3       Post
 122   Gr1       1       2        Post
 122   Gr1       3      1,9       Post
 122   Gr1       2      2,3       Post
 123   Gr2       1       2        Post
 123   Gr2       3      1,5       Post
 123   Gr2       2      2,3       Post
 123   Gr2       4       3        Post

So far, I can only manage to separate targets OR responses. For example, the code I've tried for the targets;

longdf <- pivot_longer( df, 
                        cols = c("Target_Pre", "Target_Post"),
                        names_to = c("Condition"),
                        names_pattern = "Target_(.*)" , values_to = "Targets")

When I try the same code for responses, then I have two columns for conditions in a different order.

Is there a way to use pivot_longer for both targets and responses, but keeping the conditions in one column?

CodePudding user response:

We could use the names_sep argument of pivot_longer from tidyr package:

Or if you prefer names_pattern argument: Then the regex could be this way: names_pattern = '(.*)_(.*)'

library(dplyr)
library(tidyr)
df %>%
  pivot_longer(
    -c(SubjNo, Group),
    names_to =c(".value","Condition"),
    names_sep ="_"
    )
  SubjNo Group Condition Target Response
    <int> <chr> <chr>      <int> <chr>   
 1    122 Gr1   Pre            3 2,5     
 2    122 Gr1   Post           4 3,3     
 3    122 Gr1   Pre            2 1,8     
 4    122 Gr1   Post           1 2       
 5    122 Gr1   Pre            1 1,7     
 6    122 Gr1   Post           3 1,9     
 7    122 Gr1   Pre            4 3,5     
 8    122 Gr1   Post           2 2,3     
 9    123 Gr2   Pre            4 2,6     
10    123 Gr2   Post           1 2       
11    123 Gr2   Pre            1 1,4     
12    123 Gr2   Post           3 1,5     
13    123 Gr2   Pre            3 2,6     
14    123 Gr2   Post           2 2,3     
15    123 Gr2   Pre            2 1,5     
16    123 Gr2   Post           4 3      

data:

structure(list(SubjNo = c(122L, 122L, 122L, 122L, 123L, 123L, 
123L, 123L), Group = c("Gr1", "Gr1", "Gr1", "Gr1", "Gr2", "Gr2", 
"Gr2", "Gr2"), Target_Pre = c(3L, 2L, 1L, 4L, 4L, 1L, 3L, 2L), 
    Response_Pre = c("2,5", "1,8", "1,7", "3,5", "2,6", "1,4", 
    "2,6", "1,5"), Target_Post = c(4L, 1L, 3L, 2L, 1L, 3L, 2L, 
    4L), Response_Post = c("3,3", "2", "1,9", "2,3", "2", "1,5", 
    "2,3", "3")), class = "data.frame", row.names = c(NA, -8L
))

CodePudding user response:

library(dplyr)
library(tidyr)
# Create reproducible dataset
df <- tribble(~SubjNo, ~Group,~Target_Pre,~Response_Pre,~ Target_Post,~ Response_Post,
 122, "Gr1", 3, "2,5", 4, "3,3",
 122, "Gr1", 2, "1,8", 1, "2",
 122, "Gr1", 1, "1,7", 3, "1,9",
 122, "Gr1", 4, "3,5", 2, "2,3",
 123, "Gr2", 4, "2,6", 1, "2",
 123, "Gr2", 1, "1,4", 3, "1,5",
 123, "Gr2", 3, "2,6", 2, "2,3",
 123, "Gr2", 2, "1,5", 4, "3")

# Pivot longer
df |>
 pivot_longer(cols = -c("SubjNo", "Group"),
 names_to = c(".value", "Condition"),
 names_sep = "_") |> 
 arrange(desc(Condition))

Output:

#> # A tibble: 16 x 5
#>    SubjNo Group Condition Target Response
#>     <dbl> <chr> <chr>      <dbl> <chr>   
#>  1    122 Gr1   Pre            3 2,5     
#>  2    122 Gr1   Pre            2 1,8     
#>  3    122 Gr1   Pre            1 1,7     
#>  4    122 Gr1   Pre            4 3,5     
#>  5    123 Gr2   Pre            4 2,6     
#>  6    123 Gr2   Pre            1 1,4     
#>  7    123 Gr2   Pre            3 2,6     
#>  8    123 Gr2   Pre            2 1,5     
#>  9    122 Gr1   Post           4 3,3     
#> 10    122 Gr1   Post           1 2       
#> 11    122 Gr1   Post           3 1,9     
#> 12    122 Gr1   Post           2 2,3     
#> 13    123 Gr2   Post           1 2       
#> 14    123 Gr2   Post           3 1,5     
#> 15    123 Gr2   Post           2 2,3     
#> 16    123 Gr2   Post           4 3
  • Related