I have a data frame with participants (ID) who answered several questionnaires consecutively (each row is a questionnaire). All of them started with a "general"-questionnaire and then answered pairs of "pre" and "post"-questionnaire (column "Order"). Column "Value" shows example data (there are many more columns with data, and many more participants). The amount of answered "pairs" are different among participants.
ID Order Value
1 1 general 1
2 1 pre 3
3 1 post 4
4 1 post 7
5 1 pre 0
6 1 post 10
7 2 general 1
8 2 post 0
9 2 pre 12
10 3 general 12
11 3 pre 3
12 3 post 4
13 3 pre 6
14 3 pre 8
Example data:
df1 <- data.frame("ID" = as.factor(c('1', '1', '1', '1', '1', '1', '2', '2', '2', '3', '3', '3', '3', '3')), "Order" = as.factor(c('general', 'pre', 'post', 'post', 'pre', 'post', 'general', 'post', 'pre', 'general', 'pre', 'post', 'pre', 'pre')), "Value" = as.numeric(c('1', '3','4','7','0','10', '1','0','12', '12', '3', '4', '6','8')))
Problem: Some participants forgot/failed to answer a pre-questionnaire of a pre/post-pair, others forgot/failed to answer a post-questionnaire of a pre/post-pair.
Aim: I need to add a "pre"-row or a "post"-row for each pair which is not complete. Hence, the consecutive rows should always read pre post pre post pre post etc. The added row should include the ID as well as the value from the existing part of the pair.
> df2
ID Order Value
1 1 general 1
2 1 pre 3
3 1 post 4
4 1 pre 7
5 1 post 7
6 1 pre 0
7 1 post 10
8 2 general 1
9 2 pre 0
10 2 post 0
11 2 pre 12
12 2 post 12
13 3 general 12
14 3 pre 3
15 3 post 4
16 3 pre 6
17 3 post 6
18 3 pre 8
19 3 post 8
See example data here:
df2 <- data.frame("ID" = as.factor(c('1', '1', '1', '1', '1', '1', '1', '2', '2', '2', '2', '2', '3', '3', '3', '3', '3', '3', '3')), "Order" = as.factor(c('general', 'pre', 'post', 'pre', 'post', 'pre', 'post', 'general', 'pre', 'post', 'pre', 'post', 'general', 'pre', 'post', 'pre', 'post', 'pre', 'post')), "Value" = as.numeric(c('1', '3', '4', '7', '7', '0', '10', '1', '0', '0', '12', '12', '12', '3', '4', '6', '6', '8', '8')))
The amount of pre/post-pairs can be different for each participant.
I asked a similar question here - but this did not work for this particular case. The other suggested solution also did not. I tried different versions of the complete()-function and expand.grid.