The table
function does the tabulation that you need. It returns a named vector, and you can compare the names against the column e$Intervals
to assign the correct values.
This relies on the fact that the order of the factor levels is the same in e$Intervals
and b1
and b2
. This is so because these all come from cut
.
e$b1[e$Intervals %in% names(table(b1))] <- table(b1)
e$b2[e$Intervals %in% names(table(b2))] <- table(b2)
e
## Intervals b1 b2
## 1 (-Inf,0] NA NA
## 2 (0,2] NA NA
## 3 (2,4] NA NA
## 4 (4,6] 16 9
## 5 (6,8] 3 0
## 6 (8,10] NA NA
## 7 (10,12] NA NA
## 8 (12, Inf] NA NA