| person |
week |
wage |
| a |
1 |
10 |
| a |
2 |
11 |
| b |
1 |
11 |
| b |
2 |
15 |
| c |
1 |
17 |
| c |
2 |
18 |
How can I add rows for the person "d" if there are no data available to him? That is, when my data frame does not have any data for "d", I want to have rows for "d" and assign zero as wage. Here is my desired output when "d" is missing:
| person |
week |
wage |
| a |
1 |
10 |
| a |
2 |
11 |
| b |
1 |
11 |
| b |
2 |
15 |
| c |
1 |
17 |
| c |
2 |
18 |
| d |
1 |
0 |
| d |
2 |
0 |
library(dplyr)
# When person "d" is missing
df1 <- tibble(
person = rep(c("a", "b", "c"), each = 2),
week = rep(1:2, times = 3),
wage = c(10, 11, 11, 15, 17, 18)
)
# When person "d" is not missing (code will not do anything)
df2 <- tibble(
person = rep(c("a", "b", "c", "d"), each = 2),
week = rep(1:2, times = 4),
wage = c(10, 11, 11, 15, 17, 18, 12, 11)
)