1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| > data <- data.frame(a=letters[1:5][round(runif(30, 0.2, 1) * 5)], B=LETTERS[1:4][round(runif(30, 0.25, 1) * 4)])
> tab <- table(data$a, data$B)
> tab
A B C D
a 1 1 3 0
b 0 2 3 2
c 2 3 1 1
d 0 2 2 0
e 3 2 2 0
> x <- apply(tab, 1, function(col) c(which.max(col), max(col))); colnames(x) <- NULL
> y <- apply(tab, 2, function(row) c(which.max(row), max(row))); colnames(y) <- NULL
> c(row=which.max(x[2, ]), col=x[1, which.max(x[2, ])]) ## Priorité aux colonnes.
row col
1 3
> c(row=y[1, which.max(y[2, ])], col=which.max(y[2, ])) ## Priorité aux lignes.
row col
5 1 |
Partager