-1

How do I change this table from wide to long format in R? I have tried a few different libraries but can't seem to get it.

 PatientID BaselineSBP FollowupSBP LASTSBP
 a                 110          99      98
 b                 100          95      96
 c                 104         108     103
 d                 109         100     113
Ian Campbell
  • 23,484
  • 14
  • 36
  • 57
Jackson
  • 483
  • 5
  • 20

1 Answers1

1

One approach is with tidyr:

library(tidyr)
pivot_longer(data, -PatientID)
# A tibble: 12 x 3
   PatientID name        value
   <chr>     <chr>       <dbl>
 1 a         BaselineSBP   110
 2 a         FollowupSBP    99
 3 a         LASTSBP        98
 4 b         BaselineSBP   100
 5 b         FollowupSBP    95
 6 b         LASTSBP        96
 7 c         BaselineSBP   104
 8 c         FollowupSBP   108
 9 c         LASTSBP       103
10 d         BaselineSBP   109
11 d         FollowupSBP   100
12 d         LASTSBP       113
Ian Campbell
  • 23,484
  • 14
  • 36
  • 57