Dear list, Please find below an example of odd behaviour of the by function. It occurs both under GNU/Linux R 2.6.2 and Windows R 2.7.0alpha. Respective sessionInfo()'s are given below. I hope I do not overlook anything. testFactor <- factor(sample(LETTERS[1:6], size = 42, replace = TRUE)) testMatrix <- matrix(rnorm(42 * 6), nrow = 42) testDf <- as.data.frame(testMatrix) by(data = testDf, INDICES = testFactor, FUN = mean) # OK by(data = testDf, INDICES = testFactor, FUN = median) # error # Error in median.default(data[x, ], ...) : need numeric data Kind regards, Tobias ### GNU/Linux ###> sessionInfo()R version 2.6.2 (2008-02-08) i486-pc-linux-gnu locale: LC_CTYPE=en_US.UTF-8;LC_NUMERIC=C;LC_TIME=en_US.UTF-8;LC_COLLATE=en_US.UTF-8;LC_MONETARY=en_US.UTF-8;LC_MESSAGES=en_US.UTF-8;LC_PAPER=en_US.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_US.UTF-8;LC_IDENTIFICATION=C attached base packages: [1] tools stats graphics grDevices utils datasets methods [8] base other attached packages: [1] Biobase_1.16.3 ### Windows ###> sessionInfo()R version 2.7.0 alpha (2008-03-29 r44988) i386-pc-mingw32 locale: LC_COLLATE=English_United Kingdom.1252;LC_CTYPE=English_United Kingdom.1252;LC_MONETARY=English_United Kingdom.1252;LC_NUMERIC=C;LC_TIME=English_United Kingdom.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base>
Tobias Verbeke wrote:> Dear list, > > Please find below an example of odd > behaviour of the by function. > > It occurs both under GNU/Linux R 2.6.2 > and Windows R 2.7.0alpha. Respective > sessionInfo()'s are given below. > > I hope I do not overlook anything. > > testFactor <- factor(sample(LETTERS[1:6], size = 42, replace = TRUE)) > testMatrix <- matrix(rnorm(42 * 6), nrow = 42) > testDf <- as.data.frame(testMatrix) > > by(data = testDf, INDICES = testFactor, FUN = mean) # OK > by(data = testDf, INDICES = testFactor, FUN = median) # error > # Error in median.default(data[x, ], ...) : need numeric data >The infelicity (if any) is not in by(). Try mean(airquality) median(airquality) The FUN in by() is applied to the subsetted data frame, and there is no median.data.frame method. -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907