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

Mitchell Maltenfort mmalten at gmail.com
Thu May 26 23:31:25 CEST 2011


Summary (df) will also work.

On 5/26/11, Christoph Jäckel <christoph.jaeckel at wi.tum.de> 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
>>
>> sessionInfo()
> R version 2.13.0 (2011-04-13)
> Platform: x86_64-pc-mingw32/x64 (64-bit)
>
> locale:
> [1] C
>
> attached base packages:
> [1] graphics  grDevices datasets  stats     utils     methods   base
>
> other attached packages:
> [1] urca_1.2-5      zoo_1.6-5       svSocket_0.9-51
>
> loaded via a namespace (and not attached):
> [1] grid_2.13.0     lattice_0.19-23 svMisc_0.9-61   tcltk_2.13.0
> tools_2.13.0
>
>
> Thanks Christoph
> --
> --------------------------------------------------------------------------------------------------------------------------------------------------------------------
>
> 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
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

-- 
Sent from my mobile device

Due to the recession, requests for instant gratification will be
deferred until arrears in scheduled gratification have been satisfied.



More information about the R-help mailing list