jackahall
jackahall

Reputation: 490

R: pivot longer sets of columns

I'm generating some data as follows:

timepoints_weeks <- c(seq(2, 12, 2), 16, 20, 24, 52)
timepoints_days <- 7 * timepoints_weeks
timepoints_window <- c(rep(3, 2), rep(7, 6), rep(28, 2))

mean_a <- c(70, 65, 63, 55, 45, 23, 35, 33, 29, 27)
sd_a <- rep(11, 10)
n_a <- round(309 * exp(-timepoints_days / 365) + rnorm(10, 0, timepoints_days / 30))

mean_b <- c(70, 65, 63, 55, 45, 23, 35, 33, 29, 27) + round(rnorm(10, 0, 4))
sd_b <- rep(11, 10)
n_b <- round(309 * exp(-timepoints_days / 365) + rnorm(10, 0, timepoints_days / 30))

data.frame(timepoints_days,
           timepoints_window,
           mean_a,
           sd_a,
           n_a,
           mean_b,
           sd_b,
           n_b)

This gives me a table like:

   timepoints_days timepoints_window mean_a sd_a n_a mean_b sd_b n_b
1               14                 3     70   11 297     69   11 298
2               28                 3     65   11 284     73   11 285
3               42                 7     63   11 273     64   11 276
4               56                 7     55   11 264     53   11 264
5               70                 7     45   11 256     46   11 254
6               84                 7     23   11 245     21   11 247
7              112                 7     35   11 230     34   11 228
8              140                 7     33   11 203     28   11 214
9              168                28     29   11 188     27   11 205
10             364                28     27   11 108     22   11 115

I want to pivot this into longer format, so I have the columns: timepoints_days, timepoints_window, arm, mean, sd, n, effectively pivoting the mean_a, mean_b into the columns arm, mean, and the same for sd and n.

Upvotes: 0

Views: 61

Answers (1)

Ma&#235;l
Ma&#235;l

Reputation: 52399

library(tidyr)
pivot_longer(df, -starts_with("timepoints"),
             names_sep = "_", names_to = c(".value", "arm"))

output

# A tibble: 20 × 6
   timepoints_days timepoints_window arm    mean    sd     n
             <dbl>             <dbl> <chr> <dbl> <dbl> <dbl>
 1              14                 3 a        70    11   298
 2              14                 3 b        70    11   297
 3              28                 3 a        65    11   287
 4              28                 3 b        66    11   286
 5              42                 7 a        63    11   276
 6              42                 7 b        71    11   275
 7              56                 7 a        55    11   264
 8              56                 7 b        54    11   263
 9              70                 7 a        45    11   254
10              70                 7 b        34    11   255
11              84                 7 a        23    11   245
12              84                 7 b        24    11   249
13             112                 7 a        35    11   223
14             112                 7 b        31    11   221
15             140                 7 a        33    11   208
16             140                 7 b        30    11   215
17             168                28 a        29    11   199
18             168                28 b        33    11   188
19             364                28 a        27    11   108
20             364                28 b        30    11   117

Upvotes: 1

Related Questions