With the data included below, the first bit of which looks like
head(dat, 9)
IndID BinID Freq
1 BHS_034_A 7 20
2 BHS_034_A 8 27
3 BHS_034_A 9 67
4 BHS_034_A 10 212
5 BHS_037_A 5 1
6 BHS_037_A 7 12
7 BHS_037_A 8 65
8 BHS_037_A 9 122
9 BHS_037_A 10 301
I want to fill in missing numbers of BinID
so that all individuals (IndID
) have a BinID
sequence from 1 to 10. Freq
values should be 0 when new values of BinID
are added.
I hope to accommodate many individuals, but have only included a few here.
This question, is similar to another post but here I am trying to also add 0 to the filled in columns.
The data:
dat <- structure(list(IndID = c("BHS_034_A", "BHS_034_A", "BHS_034_A",
"BHS_034_A", "BHS_037_A", "BHS_037_A", "BHS_037_A", "BHS_037_A",
"BHS_037_A", "BHS_068_A", "BHS_068_A", "BHS_068_A", "BHS_068_A",
"BHS_068_A", "BHS_068_A", "BHS_068_A", "BHS_070_A", "BHS_070_A",
"BHS_070_A", "BHS_071_A", "BHS_071_A", "BHS_071_A", "BHS_071_A",
"BHS_071_A", "BHS_071_A", "BHS_071_A", "BHS_071_A", "BHS_071_A"
), BinID = c(7L, 8L, 9L, 10L, 5L, 7L, 8L, 9L, 10L, 3L, 4L, 5L,
7L, 8L, 9L, 10L, 8L, 9L, 10L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L,
10L), Freq = c(20L, 27L, 67L, 212L, 1L, 12L, 65L, 122L, 301L,
2L, 1L, 1L, 4L, 14L, 104L, 454L, 7L, 90L, 470L, 6L, 11L, 11L,
7L, 18L, 19L, 15L, 31L, 344L)), .Names = c("IndID", "BinID",
"Freq"), row.names = c(NA, 28L), class = "data.frame")