I have a matrix with varing number of columns of the following form:
1 10 10 10 15
2 14 14 13 13
4 19 19 20 21
6 32 32 20 15
I would like to select the majority for each row producing the following output:
1 10
2 14/13
3 19
4 32
Seemed like table
almost gives what you need, but the output must be massaged. Compose
is an interesting way to do this:
require(functional)
apply(m, 1, Compose(table,
function(i) i==max(i),
which,
names,
function(i) paste0(i, collapse='/')
)
)
## [1] "10" "13/14" "19" "32"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With