Good afternoon, I was hoping somebody could help me out with the following. My data looks like:
data <- read.table(text="
Level Score ID Date
1 30 A 5/10/2019
2 50 A 5/11/2019
2 60 B 26/10/2019
4 90 B 26/11/2019
3 70 B 10/02/2020
4 92 C 29/09/2019
2 51 D 19/02/2020
4 95 D 19/05/2020
", header= TRUE)
I'd like to change my format from long to wide, based on the column 'ID' and 'Date'. I'd like to end up with the following data frame format:
data_wide <- read.table(text="
ID Level_Time_Pre Level_Time_Post Level_Time_M3 Score_Time_Pre Score_Time_Post Score_Time_M3
A 1 2 NA 30 50 NA
B 2 4 3 60 90 70
C 2 NA NA 92 NA NA
D 2 NA 4 51 NA 95
", header= TRUE)
The pre-measures are always a month before the post-measures and the M3 measures are always around 3 months after the post-measures. Not everyone has all the measures, some only have pre, some only post, some only M3, others have 2 of the three and some all three...Also, the ID variable in real-life is more complicated and I have around 8500 observations (so, I cannot use, "ifelse (ID = A, ...)"). I am thinking that I first needs to create a factor variable ('Time') based on the 'Date' column before I can use the dcast function. However, I don't know how to best create this variable. I have tried: data %>% group_by(ID) %>% mutate (Time = paste("Pre", "Post", "M3"))
. This will give the Pre, Post, M3 in each cell of the new column...
I hope you can help me, THANKS!