[R] The "median" function in R does not work properly.

Sundar Dorai-Raj sundar.dorai-raj at PDF.COM
Thu Jun 24 22:38:19 CEST 2004



Alexi Zubiria wrote:

> Hi, 
> 
>  
> 
> 1.) The "median" function does not work well.  Please refer to the data
> below (same data is attached as txt-delimited).  This is what I try to
> do in R:
> 
>  
> 
> median ( dataf [2:9] )  
> 
>  
> 
> I get warning:  "needs numeric data"
> 
>  
> 
> 2.) BUT if apply the median to a single vector: 
> 
>  
> 
> median ( dataf [,2]] )
> 
>  
> 
> then it works:
> 
>  
> 
>  
> 
> 3.) How come the "median" function does not let me take the median of
> all 8 vectors at once?  Obviously, if I can perform the test on a single
> array, then it means I have numeric data because I do not get warning.
> Also, if I look at the "mode" for each array I get "numeric".
> 
>  
> 
> 

median *does* work properly and as documented:

Arguments:

        x: a numeric vector containing the values whose median is to be
           computed.

so you supplied a `list' or `data.frame' not a vector. I'm not sure what 
you expected, but if you want to obtain the median of each column in 
your matrix, then use ?apply:

apply(as.matrix(dataf), 2, median)

Or if you want to get the median of all vectors (a single number) then use

median(as.matrix(dataf))

I assume from your post you supplied a data.frame, however if dataf 
really is a matrix (try data.class(dataf)) then median(dataf) will 
accomplish the last line (at least on R-1.9.1 for win2000).

--sundar




More information about the R-help mailing list