The data frame 'dat' below is a subset of a larger dataset composed of outcomes for multiple studies. Studies are indexed by 'srdr_id'. Each study may have more than one 'outcome'. For each 'outcome', studies have two treatment arms (arm_number 1 and 2 with arm names in 'arm') with outcome assessed at two time points, e.g. timepoint 0 or 3.
dat <- structure(list(srdr_id = c("172600", "172600", "172600", "172600",
"172600", "172600", "172600", "172600"), arm_number = c(1L, 2L,
1L, 2L, 1L, 2L, 1L, 2L), arm = c("Fluoxetine_CBT_MI", "Placebo_CBT_MI",
"Fluoxetine_CBT_MI", "Placebo_CBT_MI", "Fluoxetine_CBT_MI", "Placebo_CBT_MI",
"Fluoxetine_CBT_MI", "Placebo_CBT_MI"), outcome = c("alcohol use days",
"alcohol use days", "alcohol use days", "alcohol use days", "BDI",
"BDI", "BDI", "BDI"), timepoint = c("0", "0", "3", "3", "0",
"0", "3", "3"), timepoint_units = c("months", "months", "months",
"months", "months", "months", "months", "months"), n = c(24,
26, 24, 26, 24, 26, 24, 26), mean = c(3.01, 3.19, 1.88, 1.87,
17.25, 22.12, 6.79, 10.46), sd = c(1.75, 1.35, 1.52, 1.43, 8.87,
7.5, 7.49, 10.8)), row.names = c(NA, -8L), class = c("tbl_df",
"tbl", "data.frame"))
My goal is to create a "wide" data set as shown below, where "pre" are the timepoint = 0 values of mean, sd and n, and "post" are the corresponding timepoint = 3 values.
I considered creating list column using group_split(srdr_ooutcome), then use pivot_wider() on each element of the list column. Appreciate suggestions regarding tidyverse or base R approaches.
