Hello everyone,
I have to export my final dataset to an excel sheet. Therefore I would like to restructure the dataframe.
The dataset includes different groups (here indicated as Patient1 and Patient2). This groups were treated (treatment_1 and treatment_2) and together with the suitable controls (control_1 for treatment_1, control_2 belonging to treatment_2) measured. All parameters were detected 4 times (including outliers, indicated as NA).
The aim is to get a dataframe showing all treatment and control groups (= Sample) in the 1st column, followed by the 4 measurements of each patient (Please see "wished output").
I already tried to use pivot_wider (tidyr) but failed to get correct output.
Would be nice if anyone can suggest a solution for this problem.
Thanks in advance.
Original dataset:
df <- data.frame(Sample = rep(c("treatment_1", "control_1",
"treatment_2", "control_2"), each = 4),
Patient1 = rep(c("1"), each = 16),
Patient2 = c(1.1, 1.8, NA, 1, 1.1, 1.45, 1.32, 1.2, 1.1,
1.4, 1.6, 1.1, NA, 1.22, 1.21, 1.2))
"wished output" should look like:
df_aim <- data.frame(Sample = rep(c("treatment_1", "control_1",
"treatment_2", "control_2")),
Patient1_1 = rep(c("1"), each = 4),
Patient1_2 = rep(c("1"), each = 4),
Patient1_3 = rep(c("1"), each = 4),
Patient1_4 = rep(c("1"), each = 4),
Patient2_1 = c(1.1, 1.1, 1.1, NA),
Patient2_2 = c(1.8, 1.45, 1.4, 1.22),
Patient2_3 = c(NA, 1.32, 1.6, 1.21),
Patient2_4 = c(1, 1.2, 1.1, 1.2))