[R] help with median for each row

Erik Iverson eiverson at NMDP.ORG
Fri Aug 21 18:55:14 CEST 2009


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) 


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: 


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


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
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list