[Rd] The function 'apply' (PR#2633)
Peter Dalgaard BSA
p.dalgaard at biostat.ku.dk
Fri Mar 14 00:09:42 MET 2003
louisept at 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 at biostat.ku.dk) FAX: (+45) 35327907
More information about the R-devel
mailing list