Hi, I have what I think is a simple question/issue but I have unable to find the answer for it either in the R-manual or by browsing the web. I would like to know if there is a "mode" function in R, such that from a vector or matrix the function returns the most common value. In other programs I use (such as Matlab) I can have for exampe a 3X3 matrix A, A=[1 2 3; 1 3 2; 3 3 2] and I can find the mode of the rows so that mode(A[1:3,:]) returns a 1X3 matrix [1 3 2]. Is this possible in R as well? If not, how would one go about this? Thank you very much, Bill [[alternative HTML version deleted]]
On Aug 31, 2010, at 5:19 PM, Iain Martyn wrote:> > Hi, I have what I think is a simple question/issue but I have unable > to find the answer for it either in the R-manual or by browsing the > web. > > > > I would like to know if there is a "mode" function in R, such that > from a vector or matrix the function returns the most common value. > In other programs I use (such as Matlab) I can have for exampe a 3X3 > matrix A, A=[1 2 3; 1 3 2; 3 3 2] and I can find the mode of the > rows so that mode(A[1:3,:]) returns a 1X3 matrix [1 3 2].Matlab must use row major order? Otherwise you statement doesn't make sense. (You are going to need to adjust to R's default column major order, if that's the case.)> > > > Is this possible in R as well? If not, how would one go about this?Have you considered using names(), which.max() and table()? A=matrix(c(1, 2, 3, 1, 3, 2, 3, 3, 2), 3 ,3, byrow=TRUE) apply(A, 2, function(x) names(which.max(table(x)) )) # [1] "1" "3" "2" You cannot use just which.max(table(.)) since that would return the counts. If you want the number, then you need to wrap as.numeric around the whole mess. -- David Winsemius, MD West Hartford, CT
R does have a mode function, but it seems NOT to do the same thing as in matlab. > A <- matrix(c(1:3,1,3,2,3,3,2),nrow=3,byrow=F) > A [,1] [,2] [,3] [1,] 1 1 3 [2,] 2 3 3 [3,] 3 2 2 > A[,2] [1] 1 3 2 > mode(A) [1] "numeric" > mode(A[,2]) [1] "numeric" On 2010-9-1 5:19, Iain Martyn wrote:> Hi, I have what I think is a simple question/issue but I have unable to find the answer for it either in the R-manual or by browsing the web. > > > > I would like to know if there is a "mode" function in R, such that from a vector or matrix the function returns the most common value. In other programs I use (such as Matlab) I can have for exampe a 3X3 matrix A, A=[1 2 3; 1 3 2; 3 3 2] and I can find the mode of the rows so that mode(A[1:3,:]) returns a 1X3 matrix [1 3 2]. > > Is this possible in R as well? If not, how would one go about this? > > Thank you very much, > > Bill > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org 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. >
On Aug 31, 2010, at 10:48 PM, Dejian Zhao wrote:> R does have a mode function, but it seems NOT to do the same thing > as in matlab.Correct. It does not return the value of a vector with maximal proportions of individual elements. ?mode (It appears that the Matlab formalism fn(Name[vec, :]) may be similar to the apply function, but that is just a guess from what was written. There is an "R for Matlab Users" document by David Hiebeler that should be easy to find with a Google search. Probably also in the contributed docs.) -- David> > > A <- matrix(c(1:3,1,3,2,3,3,2),nrow=3,byrow=F) > > A > [,1] [,2] [,3] > [1,] 1 1 3 > [2,] 2 3 3 > [3,] 3 2 2 > > A[,2] > [1] 1 3 2 > > mode(A) > [1] "numeric" > > mode(A[,2]) > [1] "numeric" > > > On 2010-9-1 5:19, Iain Martyn wrote: >> Hi, I have what I think is a simple question/issue but I have >> unable to find the answer for it either in the R-manual or by >> browsing the web. >> >> >> >> I would like to know if there is a "mode" function in R, such that >> from a vector or matrix the function returns the most common >> value. In other programs I use (such as Matlab) I can have for >> exampe a 3X3 matrix A, A=[1 2 3; 1 3 2; 3 3 2] and I can find the >> mode of the rows so that mode(A[1:3,:]) returns a 1X3 matrix [1 3 2]. >> >> Is this possible in R as well? If not, how would one go about this? >> >> Thank you very much, >> >> Bill >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help at r-project.org 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. >> > > ______________________________________________ > R-help at r-project.org 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.