2

I have a dataset of adolescents over 3 waves. I need to reshape the data from wide to long, but I haven't been able to figure out how to use pivot_longer (I've checked other questions, but maybe I missed one?). Below is sample data:

HAVE DATA:

id c1sports c2sports c3sports c1smoker c2smoker c3smoker c1drinker c2drinker c3drinker 
 1       1        1        1        1        1        4         1         5         2
 2       1        1        1        5        1        3         4         1         4
 3       1        0        0        1        1        5         2         3         2
 4       0        0        0        1        3        3         4         2         3
 5       0        0        0        2        1        2         1         5         3
 6       0        0        0        4        1        4         4         3         1
 7       1        0        1        2        2        3         1         4         1
 8       0        1        1        4        4        1         4         5         4
 9       1        1        1        3        2        2         3         4         2
10       0        1        0        2        5        5         4         2         3

WANT DATA:

id wave sports smoker drinker
 1   1      1      1       1
 1   2      1      1       5
 1   3      1      4       2
 2   1      1      5       4
 2   2      1      1       1
 2   3      1      3       4
 3   1      1      1       2
 3   2      0      1       3
 3   3      0      5       2
 4   1      0      1       4
 4   2      0      3       2
 4   3      0      3       3
 5   1      0      2       1
 5   2      0      1       5
 5   3      0      2       3
 6   1      0      4       4
 6   2      0      1       3
 6   3      0      4       1
 7   1      1      2       1
 7   2      0      2       4
 7   3      1      3       1
 8   1      0      4       4
 8   2      1      4       5
 8   3      1      1       4
 9   1      1      3       3
 9   2      1      2       4
 9   3      1      2       2
10   1      0      2       4
10   2      1      2       2
10   3      0      5       3

So far the only think that I've been able to run is:

long_dat <- wide_dat %>% 
            pivot_longer(., cols = c1sports:c3drinker)

But this doesn't get me separate columns for sports, smoker, drinker.

L. Tucker
  • 523
  • 2
  • 12

1 Answers1

6

You could use names_pattern argument in pivot_longer.

tidyr::pivot_longer(df, 
                    cols = -id, 
                    names_to = c('wave', '.value'), 
                    names_pattern = 'c(\\d+)(.*)')

#     id wave  sports smoker drinker
#   <int> <chr>  <int>  <int>   <int>
# 1     1 1          1      1       1
# 2     1 2          1      1       5
# 3     1 3          1      4       2
# 4     2 1          1      5       4
# 5     2 2          1      1       1
# 6     2 3          1      3       4
# 7     3 1          1      1       2
# 8     3 2          0      1       3
# 9     3 3          0      5       2
#10     4 1          0      1       4
# … with 20 more rows
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213