As you want to know the value of higher frequency on each line, I would suggest making one lapply
of function table
per line and then check the highest frequency value:
sapply(lapply(split(a, row(a)), table), function(x) names(which.max(x)))
[1] "2" "1" "2" "2" "2" "1" "1" "1" "1" "1" "1" "1" "2" "2" "1" "1" "1" "1" "2" "1" "2"
[22] "1" "1" "2" "1" "1" "2" "1" "1" "1" "2" "1" "2" "2" "2" "2" "1" "1" "1" "2" "2" "1"
[43] "2" "2" "1" "2" "2" "1" "2" "1" "1" "1" "1" "2" "1" "2" "1" "2" "1" "2" "1" "2" "2"
[64] "1" "2" "1" "2" "1" "2" "1" "2" "2" "2" "2" "2" "1" "1" "2" "2" "1" "2" "2" "1" "2"
[85] "1" "2" "1" "2" "2" "1" "1" "1" "1" "2" "1" "2" "1" "1" "1" "2"
Demonstrating that the results are equal to your:
all.equal(combinado, as.numeric(sapply(lapply(split(a, row(a)), table), function(x) names(which.max(x)))))
[1] TRUE
Note that with the apply
now the calculation does not depend on the number of columns of the matrix. Making with M = 5
:
M = 5
a = matrix(sample(1:2, 300, replace = T), ncol = M)
sapply(lapply(split(a, row(a)), table), function(x) names(which.max(x)))
[1] "2" "2" "1" "2" "2" "2" "2" "2" "2" "1" "2" "1" "2" "2" "2" "2" "1" "2" "2" "1" "2"
[22] "1" "1" "2" "1" "2" "1" "1" "1" "2" "1" "2" "2" "2" "1" "1" "1" "2" "2" "2" "1" "2"
[43] "2" "2" "2" "2" "1" "1" "2" "1" "2" "2" "1" "2" "2" "1" "2" "2" "2" "2"
Carlos, in your solution if we increase the size from M to 11, for example, it creates an empty list.
– Wagner Jorge
@Wagnerjorge perfect Wagner, it really happens because
apply
tries to simplify the result for an array. I hadn’t thought of this situation. To prevent this from happening it is enough to use thelapply
on the lines instead of theapply
, I changed the answer.– Carlos Cinelli