Hi, I tried looking through google search on whether there's a way to computer the median for each row of a nxn matrix and return the medians for each row for further computation. And also if the number of columns in the matrix are even, how could I specify which median to use? Thank you very much! -- Edward Chen [[alternative HTML version deleted]]
?apply specifically the MARGINS argument On Fri, Aug 21, 2009 at 11:50 AM, Edward Chen<edchen51 at gmail.com> wrote:> Hi, > > I tried looking through google search on whether there's a way to computer > the median for each row of a nxn matrix and return the medians for each row > for further computation. > And also if the number of columns in the matrix are even, how could I > specify which median to use? > > Thank you very much! > > -- > Edward Chen > > ? ? ? ?[[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. >-- Stephen Sefick Let's not spend our time and resources thinking about things that are so little or so large that all they really do for us is puff us up and make us feel like gods. We are mammals, and have not exhausted the annoying little problems of being mammals. -K. Mullis
Edward, In general, if you have an nxn matrix, you can use the "apply" function to apply a function to each row of the matrix, and return the result. So, as a start, you could do, apply(your.mat, 1, median) or apply(your.mat, 1, median, na.rm = TRUE) if you want to pass further arguments to median... You can also write your own function and pass it in, of course: E.g., apply(your.mat, 1, function(x) sum(x)+1001) See ?apply. -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Edward Chen Sent: Friday, August 21, 2009 11:50 AM To: r-help at r-project.org Subject: [R] help with median for each row Hi, I tried looking through google search on whether there's a way to computer the median for each row of a nxn matrix and return the medians for each row for further computation. And also if the number of columns in the matrix are even, how could I specify which median to use? Thank you very much! -- Edward Chen [[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.
Edward, See ?apply x = matrix(rnorm(100), ncol = 10) apply(x, 1, median) Hope this helps, Greg Edward Chen wrote:> Hi, > > I tried looking through google search on whether there's a way to computer > the median for each row of a nxn matrix and return the medians for each row > for further computation. > And also if the number of columns in the matrix are even, how could I > specify which median to use? > > Thank you very much! > >-- Greg Hirson ghirson at ucdavis.edu Graduate Student Agricultural and Environmental Chemistry 1106 Robert Mondavi Institute North One Shields Avenue Davis, CA 95616
Edward Chen wrote:> Hi, > > I tried looking through google search on whether there's a way to > computer > the median for each row of a nxn matrix and return the medians for > each row > for further computation. > And also if the number of columns in the matrix are even, how could I > specify which median to use?Hi Edward, You can get the default row medians by transposing the matrix and sending it to the "describe" function in the prettyR package: describe(t(my.matrix),num.desc="median") For choosing different methods of calculating the median, you can write a wrapper for the "quantile" function: new.quantile<-function(x,na.rm=TRUE) return(quantile(x,probs=0.5,type=n,na.rm=na.rm)) describe(t(my.matrix),num.desc="my.median") where "n" is the type of median calculation you want (see the help for quantile). Jim