Grokbase Groups R r-help March 2002
FAQ

data(iris)
# iris3 is first 3 rows of iris
iris3 <- iris[1:3,]
# 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?

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

Search Discussions

  • Prof Brian D Ripley at Mar 17, 2002 at 7:31 am
    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
    _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
  • Ggrothendieck at Mar 17, 2002 at 2:36 pm

    apply(iris3,1,function(x)identical(x,iris3[1,]))
    1 2 3
    FALSE FALSE FALSE

    What's wrong here?
    iris3 is not an array, so it it coerced to one via as.matrix [...]
    and that's not the same object as your iris3
    Thanks.

    Is there a way to iterate over the rows of a data
    frame without writing a loop -- that was my
    original objective.

    -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
    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
    _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
  • Ripley at Mar 17, 2002 at 2:48 pm
    On Sun, 17 Mar 2002 ggrothendieck at yifan.net wrote:
    apply(iris3,1,function(x)identical(x,iris3[1,]))
    1 2 3
    FALSE FALSE FALSE

    What's wrong here?
    iris3 is not an array, so it it coerced to one via as.matrix [...]
    and that's not the same object as your iris3
    Thanks.

    Is there a way to iterate over the rows of a data
    frame without writing a loop -- that was my
    original objective.
    No. A row of a data frame is still a data frame and therefore arbitrarily
    complex.

    In any case, in R apply() does write a loop. There are lots of legacy
    myths about the efficiencies of loops vs *apply(), but the reality it
    depends on the exact version of your S engine (and perhaps on how much
    memory you have).

    Just for the record, lapply() is the way to iterate over columns of a data
    frame.

    --
    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
    _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
  • Ggrothendieck at Mar 17, 2002 at 4:03 pm

    apply(iris3,1,function(x)identical(x,iris3[1,]))
    1 2 3
    FALSE FALSE FALSE

    What's wrong here?
    iris3 is not an array, so it it coerced to one via as.matrix [...]
    and that's not the same object as your iris3
    Is there a way to iterate over the rows of a data
    frame without writing a loop -- that was my
    original objective.
    No. A row of a data frame is still a data frame and therefore arbitrarily
    complex.
    OK. Efficiency aside,I believe it would still be nice to have this ability
    for compactness. Here are some ideas:

    1. Have an option on t(), the transpose function, that specifies
    that it should return a list of one row data frames. The above
    becomes:
    sapply( t(iris3,list.out=T), function(x) identical( x, iris3[1,] ) )

    2. Allow dist() to have two arguments and a distance function that is 0
    for identical rows and 1 for others (or even allow user specifiable
    distance functions). The above becomes:
    dist(iris3[1,],iris3,method="identical")
    or with user specifiable distance functions:
    dist(iris3[1,],iris3,user.method=identical)

    3. Have a right crossprod such that crossprod(a,b,right=T) is the
    same as a+.*t(b) and also user specifiable inner products:
    crossprod(iris3,iris3[1,],inner.prod=identical)

    -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
    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
    _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
  • Greg Snow at Mar 18, 2002 at 6:57 pm
    On Sun, 17 Mar 2002 ggrothendieck at yifan.net wrote:
    apply(iris3,1,function(x)identical(x,iris3[1,]))
    1 2 3
    FALSE FALSE FALSE

    What's wrong here?
    [snip]
    OK. Efficiency aside,I believe it would still be nice to have this ability
    for compactness. Here are some ideas:
    How about:
    sapply( seq(along=iris3[,1]), function(x,y) identical(y[x,], iris3[1,]),
    + y=iris3)
    [1] TRUE FALSE FALSE


    --
    Greg Snow, PhD Office: 223A TMCB
    Department of Statistics Phone: (801) 378-7049
    Brigham Young University Dept.: (801) 378-4505
    Provo, UT 84602 email: gls at byu.edu

    -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
    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
    _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
  • Laurent Gautier at Mar 17, 2002 at 1:08 pm

    On Sat, Mar 16, 2002 at 11:58:51PM -0500, ggrothendieck at yifan.net wrote:
    data(iris)
    # iris3 is first 3 rows of iris
    iris3 <- iris[1:3,]
    # 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?

    -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
    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
    _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._


    Could this be because 'apply' expects an 'array' as input (try 'help(apply)') ?



    Hopin' it helps,





    Laurent





    --
    --------------------------------------------------------------
    Laurent Gautier CBS, Building 208, DTU
    PhD. Student D-2800 Lyngby,Denmark
    tel: +45 45 25 24 85 http://www.cbs.dtu.dk/laurent
    -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
    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
    _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupr-help @
categoriesr
postedMar 17, '02 at 4:58a
activeMar 18, '02 at 6:57p
posts7
users4
websiter-project.org
irc#r

People

Translate

site design / logo © 2017 Grokbase