Hi,
How do you get the most common row from a matrix? If I have a matrix
like this
array(1:3,dim=c(4,5))
[,1] [,2] [,3] [,4] [,5]
[1,]12312
[2,]23123
[3,]31231
[4,]12312
in which rows 1 and 4 are similar, I
try this:
mat - array(1:3, dim = c(4, 5))
#
ind - table(apply(mat, 1, paste, collapse = /))
ind - which.max(ind)
as.numeric(strsplit(names(ind), /)[[1]])
I hope it helps.
Best,
Dimitris
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic
Try this:
a - matrix(1:3, 4, 5)
a.ag - aggregate(1:nrow(a), as.data.frame(a), length)
a.ag[which.max(a.ag$x), 1:ncol(a)]
On 11/19/06, kone [EMAIL PROTECTED] wrote:
Hi,
How do you get the most common row from a matrix? If I have a matrix
like this
array(1:3,dim=c(4,5))
[,1] [,2]
Hi Gabor,
Your version works with the small sample matrix but I didn't get it to work
with a larger 675x8 -matrix. I don't know why. I don't know the command
aggregate if there is something.
I got this one from Peter Alspach
which.max(table(paste(data.frame(t(MATRIX)), sep=',')))
and it seems