I'm making a count table for 2 data frames. One looks like this:
table1 <- data.table("Col1" = c("Al", "Al", "Al", "Cu", "Cu", "Cu", "Pb", "Pb",
"Pb"), "Col2" = c("F", "UF", "P", "F", "UF", "P", "F", "UF", "P"), "Col3" = c("C",
"UC", "<", "C", "UC", "<", "C", "UC", "<"))
table2 <- data.table("Col1" = c("Al", "Al", "Cu", "Pb", "Pb", "Pb"), "Col2" = c("F",
"UF", "F", "F", "UF", "P"), "Col3" = c("C", "UC", "<", "C", "UC", "<"))
I'd create a count table for table1 and table2 like this:
table1 %>% group_by(Col1, Col2, Col3) %>% tally()
# A tibble: 9 x 4
# Groups: Col1, Col2 [9]
Col1 Col2 Col3 n
<chr> <chr> <chr> <int>
1 Al F C 1
2 Al P < 1
3 Al UF UC 1
4 Cu F C 1
5 Cu P < 1
6 Cu UF UC 1
7 Pb F C 1
8 Pb P < 1
9 Pb UF UC 1
table2 %>% group_by(Col1, Col2, Col3) %>% tally()
# A tibble: 6 x 4
# Groups: Col1, Col2 [6]
Col1 Col2 Col3 n
<chr> <chr> <chr> <int>
1 Al F C 1
2 Al UF UC 1
3 Cu F < 1
4 Pb F C 1
5 Pb P < 1
6 Pb UF UC 1
But I'd like the count table for table2 to have the 0 counts with the combinations from table1, not remove them completely so it doesn't show the 0 counts. Is there a way I can do this with dplyr or a different package?
Thank you for your help!