I suppose this is a pretty simple questions, none the less I can't get my head around it.
Consider this data frame:
df <- data.frame(
week = c(1,1,1,2,3,3,3),
id = c(12,13,14,12,12,13,14),
x = sample(c(100:200), 7))
df
#> week id x
#> 1 1 12 126
#> 2 1 13 166
#> 3 1 14 129
#> 4 2 12 128
#> 5 3 12 136
#> 6 3 13 120
#> 7 3 14 115
I would like to create a dummy value 0 or NA
for x
for week 2 where there are no observations for id
13 and 14. In other words: so that x = 0
for week 2 id
13 and 14. Preferably with dplyr
.
Any ideas on how to do this?