简体   繁体   中英

R: creating a longitudinal dataset using tidyr

I am looking to generate a longitudinal dataset. I have generated my pat numbers and treatment groups:

library(dplyr)
set.seed(420)
Pat_TNO <- 1001:1618

data.frame(Pat_TNO = Pat_TNO) %>%
  rowwise() %>%
  mutate(
    trt = rbinom(1, 1, 0.5)
  )

My timepoints (in days) are:

timepoint_weeks <- c(seq(2, 12, 2), 16, 20, 24, 52)
timepoint_days <- 7 * timepoint_weeks

How can I pivot this dataset using the vector timepoint_days , so I have 10 rows per participant and column names Pat_TNO , trt , timepoint_days .

You can use the unnest function from tidyr to achieve what you want.

Here is the code

library(dplyr)
library(tidyr)
set.seed(420)
Pat_TNO <- 1001:1618

x <- data.frame(Pat_TNO = Pat_TNO) %>%
  rowwise() %>%
  mutate(
    trt = rbinom(1, 1, 0.5)
  )

timepoint_weeks <- c(seq(2, 12, 2), 16, 20, 24, 52)
timepoint_days <- 7 * timepoint_weeks

x %>% 
  mutate(timepoint_days = list(timepoint_days)) %>% 
  unnest()

Output

# A tibble: 6,180 × 3
   Pat_TNO   trt timepoint_days
     <int> <int>          <dbl>
 1    1001     1             14
 2    1001     1             28
 3    1001     1             42
 4    1001     1             56
 5    1001     1             70
 6    1001     1             84
 7    1001     1            112
 8    1001     1            140
 9    1001     1            168
10    1001     1            364
# … with 6,170 more rows

Here I used the mutate function to add a column with a list containing timepoint_days in every row. And then unnest collapses each row to get 10 rows per participant.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM