I have the following data frame:
library(dplyr)
library(tibble)
df <- tibble(
source = c("a", "b", "b"),
day = c("D1", "D2", "D3"),
score = c(10, 5, 3) )
df
Which looks like this:
> df
# A tibble: 3 x 3
source day score
<chr> <chr> <dbl>
1 a D1 10
2 b D2 5
3 b D3 3
Now the values for source
and day
are incomplete. The full list of source
and day
is
stored as vectors:
complete_source <- c("a", "b","c")
complete_day <- c("D1", "D2" ,"D3", "D4")
What I want to do is to complete the data frame based on complete_source
and complete_day
,
filling the value with zero (0).
The desired result is (hand made):
source day score
a D1 10
a D2 0
a D3 0
a D4 0
b D2 5
b D3 3
... etc ...
c D1 0
c D2 0
c D3 0
c D4 0
...etc
How can I achieve that?