Suppose I've got the following data.table
:
dt <- data.table(id=c(1,1,1,1,1,1,2,2,2,2),
wday=c("mon","tue","wed","thu","fri","sat","mon","tue","thu","fri"),
val=c(2,3,5,8,6,2,3,4,2,6))
id wday val
1: 1 mon 2
2: 1 tue 3
3: 1 wed 5
4: 1 thu 8
5: 1 fri 6
6: 1 sat 2
7: 2 mon 3
8: 2 tue 4
9: 2 thu 2
10: 2 fri 6
This is the result of an aggregation of another data.table
. It represents the count (val
) of a variable depending on the week day (wday
) for different individuals (id
). The problem is, during my operations I've lost the week days where the count is 0.
So the question is : how could I update my data.table
object efficiently by inserting, for each id, as many rows as there are missing week days with val=0
?
The result would be the following :
id wday val
1: 1 mon 2
2: 1 tue 3
3: 1 wed 5
4: 1 thu 8
5: 1 fri 6
6: 1 sat 2
7: 1 sun 0
8: 2 mon 3
9: 2 tue 4
10: 2 wed 0
11: 2 thu 2
12: 2 fri 6
13: 2 sat 0
14: 2 sun 0
Thanks a lot for your help.