Full_Name: Paul Louisell Version: 1.6.2 OS: Windows NT Submission from: (NULL) (192.249.47.9) I've found a problem with either the 'apply' or 'is.factor' functions, and it seems like it might be cause for concern. Here are the commands taken directly from the R session:> is.factor(test.frame[, 1])[1] TRUE> is.factor(test.frame[, 2])[1] TRUE> is.factor(test.frame[, 3])[1] TRUE> is.factor(test.frame[, 4])[1] TRUE> apply(test.frame, 2, is.factor)X1 X2 X3 X4 FALSE FALSE FALSE FALSE All of the variables in test.frame are factors, and is.factor returns TRUE when it's used on each factor individually. When it's used inside of apply, all of the variables return the value FALSE. So there's something wrong in at least one of these functions.
louisept@pweh.com writes:> I've found a problem with either the 'apply' or 'is.factor' functions, and it > seems like it might be cause for concern. Here are the commands taken directly > from the R session: > > > is.factor(test.frame[, 1]) > [1] TRUE > > is.factor(test.frame[, 2]) > [1] TRUE > > is.factor(test.frame[, 3]) > [1] TRUE > > is.factor(test.frame[, 4]) > [1] TRUE > > apply(test.frame, 2, is.factor) > X1 X2 X3 X4 > FALSE FALSE FALSE FALSE > > > All of the variables in test.frame are factors, and is.factor returns TRUE when > it's used on each factor individually. When it's used inside of apply, all of > the variables return the value FALSE. So there's something wrong in at least one > of these functions.No. Apply works on matrices, so the first thing that happens is that the data.frame is coerced to a matrix and> as.matrix(test.frame)a b c 1 "1" "1" "1" 2 "2" "2" "2" 3 "3" "3" "3" doesn't have factors as columns. Similarly> test.frame <- data.frame(a=letters[1:3],n=1:3) > as.matrix(test.frame)a n 1 "a" "1" 2 "b" "2" 3 "c" "3"> apply(test.frame,2,mode)a n "character" "character" This is quite explicitly stated in the help page for apply(). You need lapply() if you want to query dataframe elements:> lapply(test.frame,is.numeric)$a [1] FALSE $n [1] TRUE -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk) FAX: (+45) 35327907
The `something wrong' is in your reading (or lack of it). Not checking the help page before filing a bug report just wastes the developer's time. That *is* `a cause for concern': it applies to far too many bug reports. Let me read the help(apply) page for you: X: the array to be used. Details: If `X' is not an array but has a dimension attribute, `apply' attempts to coerce it to an array via `as.matrix' if it is two-dimensional (e.g. data frames) or via `as.array'. so test.frame (presumably a data frame) is coerced to a matrix and the results are correct. What I believe you intended can be done by sapply(test.frame, is.factor). If you do ever find a real bug in R, please supply a reproducible example with your report. You have omitted to tell us what test.frame is. On Thu, 13 Mar 2003 louisept@pweh.com wrote:> Full_Name: Paul Louisell > Version: 1.6.2 > OS: Windows NT > Submission from: (NULL) (192.249.47.9) > > > I've found a problem with either the 'apply' or 'is.factor' functions, and it > seems like it might be cause for concern. Here are the commands taken directly > from the R session: > > > is.factor(test.frame[, 1]) > [1] TRUE > > is.factor(test.frame[, 2]) > [1] TRUE > > is.factor(test.frame[, 3]) > [1] TRUE > > is.factor(test.frame[, 4]) > [1] TRUE > > apply(test.frame, 2, is.factor) > X1 X2 X3 X4 > FALSE FALSE FALSE FALSE > > > All of the variables in test.frame are factors, and is.factor returns TRUE when > it's used on each factor individually. When it's used inside of apply, all of > the variables return the value FALSE. So there's something wrong in at least one > of these functions.-- Brian D. Ripley, ripley@stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595