A data.table
решение:
library(data.table)
dcast(setDT(dummy)[, .(count = .N),
.(categorical_1, categorical_2)],
categorical_1~categorical_2)[,
.(categorical_1 = categorical_1,
one=paste0(one, " (", one/sum(one), ")"),
two=paste0(two, " (", one/sum(two), ")"))]
#> categorical_1 one two
#> 1: a 3 (0.6) 2 (0.6)
#> 2: b 2 (0.4) 3 (0.4)
Данные:
dummy <- data.frame(categorical_1 = c("a", "b", "a", "a", "b", "b", "a", "b", "b", "a"),
categorical_2 = c(rep("one", 5), rep("two", 5)),
numeric = sample(1:10, 10))