Let's say I have the following dataset:
ID Type Group Week Value
111 A Pepper -1 10
112 B Salt 2 20
113 C Curry 4 40
114 D Rosemary 9 90
211 A Pepper -1 15
212 B Salt 2 30
214 D Rosemary 9 135
Where ID, Type and Group as well as Week are entered in a measurement instrument measuring "value" each week. Sometimes there are multiple results per week so the initial tidying was to create a mean for each weekly measurement.
I would like to
a) create a dataset where the rows are automatically inserted where there are empty lines in the Week-column so it looks like this - always with the Type order A, B, C, D and Group order Pepper, Salt, Curry, Rosemary and Week -1, 2, 4, 9.
ID Type Group Week Value
111 A Pepper -1 10
112 B Salt 2 20
113 C Curry 4 40
114 D Rosemary 9 90
211 A Pepper -1 15
212 B Salt 2 30
213 C Curry 4 60
214 D Rosemary 9 135
b) The objective is to calculate the difference between the measured values in a vertical plane only for each group ie:
ID Type Group Week Value Diff
111 A Pepper -1 10 NA
112 B Salt 2 20 10
113 C Curry 4 40 20
114 D Rosemary 9 90 50
211 A Pepper -1 15 NA
212 B Salt 2 30 15
213 C Curry 4 60 30
214 D Rosemary 9 135 75
I can see how to do this in a for loop but there must be a more elegant way?