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

From: Joshua Wiley <jwiley.psych_at_gmail.com>
Date: Thu, 26 May 2011 14:36:16 -0700

Hi Christoph,

Use:

sapply(df, median)

median() does not have methods for a data frame (read: it is never meant to be used directly on data frames so do not expect sensical results).

Cheers,

Josh

On Thu, May 26, 2011 at 2:22 PM, 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.
>

-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/

______________________________________________
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.
Received on Thu 26 May 2011 - 21:38:16 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 26 May 2011 - 21:50:09 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive