[R] apply problem

Prof Brian D Ripley ripley at stats.ox.ac.uk
Sun Mar 17 08:31:12 CET 2002


On Sat, 16 Mar 2002 ggrothendieck at yifan.net wrote:

>
> > data(iris)
>
> # iris3 is first 3 rows of iris
> > iris3 <- iris[1:3,]

Not a good choice of name: iris3 is another R dataset.

Your iris3 is a data frame.

> # z compares row 1 to each row of iris3 and is correctly
> computed
> > z <- c(F,F,F)
> > for(i in seq(z)) z[i] <- identical(iris3[1,],iris3[i,])
> > z
> [1]  TRUE FALSE FALSE
>
> # this should do the same but is incorrect
> > apply(iris3,1,function(x)identical(x,iris3[1,]))
>     1     2     3
> FALSE FALSE FALSE
>
> What's wrong here?

You are not using array as documented. ?apply says

Arguments:

       X: the array to be used.


iris3 is not an array, so it it coerced to one via as.matrix

> as.matrix(iris3)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 "5.1"        "3.5"       "1.4"        "0.2"       "setosa"
2 "4.9"        "3.0"       "1.4"        "0.2"       "setosa"
3 "4.7"        "3.2"       "1.3"        "0.2"       "setosa"

and that's not the same object as your iris3


-- 
Brian D. Ripley,                  ripley at 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 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list