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

Christoph Jäckel christoph.jaeckel at wi.tum.de
Thu May 26 23:43:32 CEST 2011


Hey together,

thanks a lot for the quick and helpful answers. I will not use median
on a data.frame from now on and use sapply instead.

Thanks Christoph

On Thu, May 26, 2011 at 11:37 PM, Marc Schwartz <marc_schwartz at me.com> wrote:
> 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
>
>



-- 
--------------------------------------------------------------------------------------------------------------------------------------------------------------------

Christoph Jäckel (Dipl.-Kfm.)

--------------------------------------------------------------------------------------------------------------------------------------------------------------------

Research Assistant

Chair for Financial Management and Capital Markets | Lehrstuhl für
Finanzmanagement und Kapitalmärkte

TUM School of Management | Technische Universität München

Arcisstr. 21 | D-80333 München | Germany

Mailto: christoph.jaeckel at wi.tum.de | Web: www.fm.wi.tum.de

Phone: +49 89 289 25482 | Fax: +49 89 289 25488



Head of Chair:

Univ.-Prof. Dr. Christoph Kaserer

----------------------------------------------------------------------------------------------------------------------------------------------------------------------

E-Mail Disclaimer

Der Inhalt dieser E-Mail ist vertraulich und ausschliesslich
fuer den bezeichneten Adressaten bestimmt. Wenn Sie nicht
der vorgesehene Adressat dieser E-Mail oder dessen Vertreter
sein sollten, so beachten Sie bitte, dass jede Form der
Kenntnisnahme, Veroeffentlichung, Vervielfaeltigung oder
Weitergabe des Inhalts dieser E-Mail unzulaessig ist. Wir
bitten Sie, sich in diesem Fall mit dem Absender der E-Mail
in Verbindung zu setzen.

The information contained in this email is confidential....{{dropped:11}}



More information about the R-help mailing list