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,] 1 2 3 1 2 [2,] 2 3 1 2 3 [3,] 3 1 2 3 1 [4,] 1 2 3 1 2 in which rows 1 and 4 are similar, I want to find that vector c (1,2,3,1,2). Atte Tenkanen University of Turku, Finland
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 University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm Quoting kone <attenka at utu.fi>:> 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,] 1 2 3 1 2 > [2,] 2 3 1 2 3 > [3,] 3 1 2 3 1 > [4,] 1 2 3 1 2 > > in which rows 1 and 4 are similar, I want to find that vector c > (1,2,3,1,2). > > Atte Tenkanen > University of Turku, Finland > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
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 <attenka at utu.fi> 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] [,3] [,4] [,5] > [1,] 1 2 3 1 2 > [2,] 2 3 1 2 3 > [3,] 3 1 2 3 1 > [4,] 1 2 3 1 2 > > in which rows 1 and 4 are similar, I want to find that vector c > (1,2,3,1,2). > > Atte Tenkanen > University of Turku, Finland > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Here is an idea you can develop as you wish. You need to be specific about how to handly several rows that occur equally commonly, for example. mcr <- function(x, drop = FALSE) { #'most common row' xx <- do.call("paste", c(data.frame(x), sep = "\r")) tx <- table(xx) mx <- names(tx)[which(tx == max(tx))[1]] x[match(mx, xx), , drop = drop] } Slightly extend your example:> x[,1] [,2] [,3] [,4] [,5] [1,] 1 2 3 1 2 [2,] 2 3 1 2 3 [3,] 3 1 2 3 1 [4,] 1 2 3 1 2 [5,] 1 2 3 1 2 [6,] 2 3 1 2 3 [7,] 3 1 2 3 1 [8,] 1 2 3 1 2 Now:> mcr(x)[,1] [,2] [,3] [,4] [,5] [1,] 1 2 3 1 2> mcr(x, drop = T)[1] 1 2 3 1 2 ___ This kind of example suggests that it might be useful to make 'match' generic (like 'duplicated' and 'unique'), with methods for data frames and matrices. Bill Venables. -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of kone Sent: Monday, 20 November 2006 5:31 AM To: r-help at stat.math.ethz.ch Subject: [R] The most common row in a matrix? 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,] 1 2 3 1 2 [2,] 2 3 1 2 3 [3,] 3 1 2 3 1 [4,] 1 2 3 1 2 in which rows 1 and 4 are similar, I want to find that vector c (1,2,3,1,2). Atte Tenkanen University of Turku, Finland ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Yet another solution: library(cluster) x <- array(1:3,dim=c(4,5)) dissim <- as.matrix(daisy(as.data.frame(x))) dissim[!upper.tri(dissim)] <- NA unique(x[which(dissim == 0, arr.ind=TRUE), ]) HTH, Adrian On Sunday 19 November 2006 21:31, kone 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] [,3] [,4] [,5] > [1,] 1 2 3 1 2 > [2,] 2 3 1 2 3 > [3,] 3 1 2 3 1 > [4,] 1 2 3 1 2 > > in which rows 1 and 4 are similar, I want to find that vector c > (1,2,3,1,2). > > Atte Tenkanen > University of Turku, Finland-- Adrian DUSA Arhiva Romana de Date Sociale Bd. Schitu Magureanu nr.1 050025 Bucuresti sectorul 5 Romania Tel./Fax: +40 21 3126618 \ +40 21 3120210 / int.101