[R] Different behavior of median and mean function - Why?

Marc Schwartz marc_schwartz at me.com
Thu May 26 23:37:00 CEST 2011


On May 26, 2011, at 4:22 PM, Christoph Jäckel wrote:

> Hi together,
> 
> below is a small example which produces outcome I do not understand,
> namely that the median function works fine on a data.frame without
> negative numbers, but doesn't work on a data.frame with one negative
> number. I'm sure there is a reasonable explanation for that or better,
> that I'm doing something wrong and someone could guide me how to solve
> it. I tried googling it, but couldn't find a solution:
> 
>> #Set up data frame
>> df <- data.frame(V1=c(1,2,3,4),V2=c(2,3,4,5))
>> #Both work fine
>> mean(df)
> V1  V2
> 2.5 3.5
>> median(df)
> [1] 2.5 3.5
>> 
>> #Now, I just make one number negative in the data.frame
>> df <- data.frame(V1=c(1,2,3,-4),V2=c(2,3,4,5))
>> mean(df)#Works fine
> V1  V2
> 0.5 3.5
>> median(df)#Why do I  get that error?
> [1]  NA 0.5
> Warnmeldung:
> In mean.default(X[[1L]], ...) :
>  argument is not numeric or logical: returning NA
>> #It works fine on both columns seperately
>> median(df$V1)
> [1] 1.5
>> median(df$V2)
> [1] 3.5


This was actually just discussed late last month. See the thread here:

  https://stat.ethz.ch/pipermail/r-devel/2011-April/060731.html

The bottom line is that median does not have a 'method' for data frames, whereas mean does.

HTH,

Marc Schwartz



More information about the R-help mailing list