Christoph, After a quick look at the code for median, I'm amazed that it gives a correct result for any data frame.
median() isn't really intended for use with data frames; there's no data.frame method. The correct and safe approach is to use sapply(df, median) This was recently discussed on the R-devel list: http://r.789695.n4.nabble.com/median-and-data-frames-td3478921.html Sarah On Thu, May 26, 2011 at 5:22 PM, Christoph Jäckel <christoph.jaec...@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 > -- > -- Sarah Goslee http://www.functionaldiversity.org ______________________________________________ R-help@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.