[R] help with median for each row

Jim Lemon jim at bitwrit.com.au
Sat Aug 22 11:54:06 CEST 2009


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




More information about the R-help mailing list