Решение Base R может включать cut
и aggregate
.
f <- cut(dat$Col.1, c(1, 6, 11, 16, 21, Inf),
include.lowest = TRUE, right = FALSE)
agg <- aggregate(Col.2 ~ f, dat, table)
agg <- cbind(agg[1], agg[[2]])
agg[2:3] <- 100*agg[2:3]/rowSums(agg[2:3])
agg
# f No Yes
#1 [1,6) 60.00000 40.00000
#2 [6,11) 40.00000 60.00000
#3 [11,16) 50.00000 50.00000
#4 [16,21) 66.66667 33.33333
Данные.
dat <-
structure(list(Col.1 = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L,
10L, 12L, 14L, 16L, 18L, 20L), Col.2 = structure(c(2L, 1L, 2L,
1L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 2L), .Label = c("No",
"Yes"), class = "factor")), class = "data.frame", row.names = c(NA,
-15L))