gregor.gorjanc at bfro.uni-lj.si
2006-Sep-01 11:58 UTC
[Rd] [<-.POSIXlt changes order of attributes (PR#9197)
Hello! I was doing some tests with identical() and found out that [<-.POSIXlt method changes order of attributes. This example shows that: x <- strptime("1900-1-1", format="%Y-%m-%d") x <- c(x) y <- c(x, x+1) x1 <- x y1 <- y attributes(x) $names [1] "sec" "min" "hour" "mday" "mon" "year" "wday" "yday" "isdst" $class [1] "POSIXt" "POSIXlt" $tzone [1] "" "CET" "CEST" identical(attributes(x), attributes(y)) TRUE x[1] <- NA attributes(x) $names [1] "sec" "min" "hour" "mday" "mon" "year" "wday" "yday" "isdst" $tzone [1] "" "CET" "CEST" $class [1] "POSIXt" "POSIXlt" y[1] <- NA attributes(y) $names [1] "sec" "min" "hour" "mday" "mon" "year" "wday" "yday" "isdst" $tzone [1] "" "CET" "CEST" $class [1] "POSIXt" "POSIXlt" identical(attributes(x), attributes(x1)) FALSE identical(attributes(y), attributes(y1)) FALSE This can be solved with either: Index: R/src/library/base/R/datetime.R ==================================================================--- R/src/library/base/R/datetime.R (revision 39045) +++ R/src/library/base/R/datetime.R (working copy) @@ -713,10 +713,10 @@ { if(!as.logical(length(value))) return(x) value <- as.POSIXlt(value) - cl <- oldClass(x) + att <- attributes(x) class(x) <- class(value) <- NULL for(n in names(x)) x[[n]][i] <- value[[n]] - class(x) <- cl + attributes(x) <- att x } or in the same way as it is done in [.<-POSIXct method Index: R/src/library/base/R/datetime.R ==================================================================--- R/src/library/base/R/datetime.R (revision 39045) +++ R/src/library/base/R/datetime.R (working copy) @@ -714,9 +714,11 @@ if(!as.logical(length(value))) return(x) value <- as.POSIXlt(value) cl <- oldClass(x) + tz <- attr(x, "tzone") class(x) <- class(value) <- NULL for(n in names(x)) x[[n]][i] <- value[[n]] class(x) <- cl + attr(x, "tzone") <- tz x } I have checked both versions in r-devel with make check-all and all went fine. Regards, Gregor -- Lep pozdrav / With regards, Gregor Gorjanc ---------------------------------------------------------------------- University of Ljubljana PhD student Biotechnical Faculty Zootechnical Department URI: http://www.bfro.uni-lj.si/MR/ggorjan Groblje 3 mail: gregor.gorjanc <at> bfro.uni-lj.si SI-1230 Domzale tel: +386 (0)1 72 17 861 Slovenia, Europe fax: +386 (0)1 72 17 888 ---------------------------------------------------------------------- "One must learn by doing the thing; for though you think you know it, you have no certainty until you try." Sophocles ~ 450 B.C. ----------------------------------------------------------------------
Prof Brian Ripley
2006-Sep-01 15:18 UTC
[Rd] [<-.POSIXlt changes order of attributes (PR#9197)
Please point us to the documentation that says attributes are ordered. E.g. R-lang.texi says All objects except @code{NULL} can have one or more attributes attached to them. Attributes are stored as a list where all elements are named. (although in fact they are stored in a pairlist). I know of tens of functions that change the order of attributes, and did look at teaching identical() that they are not ordered. But since they are stored as a pairlist, it would be quite expensive. (Given that attributes seem to be growing in use, another internal storage mechanism is becoming more appropriate.) BTW, all.equal() does not assume attributes are ordered. On Fri, 1 Sep 2006, gregor.gorjanc at bfro.uni-lj.si wrote:> This is a multi-part message in MIME format. > --------------090203020600020104020707 > Content-Type: text/plain; charset=UTF-8 > Content-Transfer-Encoding: 7bit > > Hello! > > I was doing some tests with identical() and found out that [<-.POSIXlt > method changes order of attributes. This example shows that: > > x <- strptime("1900-1-1", format="%Y-%m-%d") > x <- c(x) > y <- c(x, x+1) > x1 <- x > y1 <- y > > attributes(x) > $names > [1] "sec" "min" "hour" "mday" "mon" "year" "wday" "yday" "isdst" > > $class > [1] "POSIXt" "POSIXlt" > > $tzone > [1] "" "CET" "CEST" > > identical(attributes(x), attributes(y)) > TRUE > > x[1] <- NA > attributes(x) > $names > [1] "sec" "min" "hour" "mday" "mon" "year" "wday" "yday" "isdst" > > $tzone > [1] "" "CET" "CEST" > > $class > [1] "POSIXt" "POSIXlt" > > y[1] <- NA > attributes(y) > $names > [1] "sec" "min" "hour" "mday" "mon" "year" "wday" "yday" "isdst" > > $tzone > [1] "" "CET" "CEST" > > $class > [1] "POSIXt" "POSIXlt" > > identical(attributes(x), attributes(x1)) > FALSE > > identical(attributes(y), attributes(y1)) > FALSE > > This can be solved with either: > > Index: R/src/library/base/R/datetime.R > ==================================================================> --- R/src/library/base/R/datetime.R (revision 39045) > +++ R/src/library/base/R/datetime.R (working copy) > @@ -713,10 +713,10 @@ > { > if(!as.logical(length(value))) return(x) > value <- as.POSIXlt(value) > - cl <- oldClass(x) > + att <- attributes(x) > class(x) <- class(value) <- NULL > for(n in names(x)) x[[n]][i] <- value[[n]] > - class(x) <- cl > + attributes(x) <- att > x > } > > or in the same way as it is done in [.<-POSIXct method > > Index: R/src/library/base/R/datetime.R > ==================================================================> --- R/src/library/base/R/datetime.R (revision 39045) > +++ R/src/library/base/R/datetime.R (working copy) > @@ -714,9 +714,11 @@ > if(!as.logical(length(value))) return(x) > value <- as.POSIXlt(value) > cl <- oldClass(x) > + tz <- attr(x, "tzone") > class(x) <- class(value) <- NULL > for(n in names(x)) x[[n]][i] <- value[[n]] > class(x) <- cl > + attr(x, "tzone") <- tz > x > } > > I have checked both versions in r-devel with make check-all and all went > fine. > > Regards, Gregor > > >-- 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 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Prof Brian Ripley
2006-Sep-02 16:41 UTC
[Rd] [<-.POSIXlt changes order of attributes (PR#9197)
There is a bug in identical(), though:> identical(pairlist(a=1, b=2), pairlist(a=1, aa=2))[1] TRUE> identical(structure(pi, a=1, b=2), structure(pi,a=1, aa=2))[1] TRUE so identical() is not even looking at the names of the attributes but only checking their values. Oops .... On Fri, 1 Sep 2006, Prof Brian Ripley wrote:> Please point us to the documentation that says attributes are ordered. > E.g. R-lang.texi says > > All objects except @code{NULL} can have one or more attributes attached > to them. Attributes are stored as a list where all elements are named. > > (although in fact they are stored in a pairlist). > > I know of tens of functions that change the order of attributes, and > did look at teaching identical() that they are not ordered. But > since they are stored as a pairlist, it would be quite expensive. > (Given that attributes seem to be growing in use, another internal storage > mechanism is becoming more appropriate.) > > BTW, all.equal() does not assume attributes are ordered. > > > On Fri, 1 Sep 2006, gregor.gorjanc at bfro.uni-lj.si wrote: > > > This is a multi-part message in MIME format. > > --------------090203020600020104020707 > > Content-Type: text/plain; charset=UTF-8 > > Content-Transfer-Encoding: 7bit > > > > Hello! > > > > I was doing some tests with identical() and found out that [<-.POSIXlt > > method changes order of attributes. This example shows that: > > > > x <- strptime("1900-1-1", format="%Y-%m-%d") > > x <- c(x) > > y <- c(x, x+1) > > x1 <- x > > y1 <- y > > > > attributes(x) > > $names > > [1] "sec" "min" "hour" "mday" "mon" "year" "wday" "yday" "isdst" > > > > $class > > [1] "POSIXt" "POSIXlt" > > > > $tzone > > [1] "" "CET" "CEST" > > > > identical(attributes(x), attributes(y)) > > TRUE > > > > x[1] <- NA > > attributes(x) > > $names > > [1] "sec" "min" "hour" "mday" "mon" "year" "wday" "yday" "isdst" > > > > $tzone > > [1] "" "CET" "CEST" > > > > $class > > [1] "POSIXt" "POSIXlt" > > > > y[1] <- NA > > attributes(y) > > $names > > [1] "sec" "min" "hour" "mday" "mon" "year" "wday" "yday" "isdst" > > > > $tzone > > [1] "" "CET" "CEST" > > > > $class > > [1] "POSIXt" "POSIXlt" > > > > identical(attributes(x), attributes(x1)) > > FALSE > > > > identical(attributes(y), attributes(y1)) > > FALSE > > > > This can be solved with either: > > > > Index: R/src/library/base/R/datetime.R > > ==================================================================> > --- R/src/library/base/R/datetime.R (revision 39045) > > +++ R/src/library/base/R/datetime.R (working copy) > > @@ -713,10 +713,10 @@ > > { > > if(!as.logical(length(value))) return(x) > > value <- as.POSIXlt(value) > > - cl <- oldClass(x) > > + att <- attributes(x) > > class(x) <- class(value) <- NULL > > for(n in names(x)) x[[n]][i] <- value[[n]] > > - class(x) <- cl > > + attributes(x) <- att > > x > > } > > > > or in the same way as it is done in [.<-POSIXct method > > > > Index: R/src/library/base/R/datetime.R > > ==================================================================> > --- R/src/library/base/R/datetime.R (revision 39045) > > +++ R/src/library/base/R/datetime.R (working copy) > > @@ -714,9 +714,11 @@ > > if(!as.logical(length(value))) return(x) > > value <- as.POSIXlt(value) > > cl <- oldClass(x) > > + tz <- attr(x, "tzone") > > class(x) <- class(value) <- NULL > > for(n in names(x)) x[[n]][i] <- value[[n]] > > class(x) <- cl > > + attr(x, "tzone") <- tz > > x > > } > > > > I have checked both versions in r-devel with make check-all and all went > > fine. > > > > Regards, Gregor > > > > > > > >-- 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 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Gregor Gorjanc
2006-Sep-02 20:02 UTC
[Rd] [<-.POSIXlt changes order of attributes (PR#9197)
Hello, Prof Brian Ripley wrote:> Please point us to the documentation that says attributes are ordered. > E.g. R-lang.texi says > > All objects except @code{NULL} can have one or more attributes attached > to them. Attributes are stored as a list where all elements are named. > > (although in fact they are stored in a pairlist). > > I know of tens of functions that change the order of attributes, and > did look at teaching identical() that they are not ordered. But > since they are stored as a pairlist, it would be quite expensive.I agree that there is no need for attributes to be always in the same order as this is tedious to achieve as you noted above, but identical() seems to care about this.> (Given that attributes seem to be growing in use, another internal storage > mechanism is becoming more appropriate.)Which internal storage mechanism are you talking about here?> BTW, all.equal() does not assume attributes are ordered.-- Lep pozdrav / With regards, Gregor Gorjanc ---------------------------------------------------------------------- University of Ljubljana PhD student Biotechnical Faculty Zootechnical Department URI: http://www.bfro.uni-lj.si/MR/ggorjan Groblje 3 mail: gregor.gorjanc <at> bfro.uni-lj.si SI-1230 Domzale tel: +386 (0)1 72 17 861 Slovenia, Europe fax: +386 (0)1 72 17 888 ---------------------------------------------------------------------- "One must learn by doing the thing; for though you think you know it, you have no certainty until you try." Sophocles ~ 450 B.C.
Gregor Gorjanc
2006-Sep-02 20:05 UTC
[Rd] [<-.POSIXlt changes order of attributes (PR#9197)
Hello, Prof Brian Ripley wrote:> There is a bug in identical(), though: > >> identical(pairlist(a=1, b=2), pairlist(a=1, aa=2)) > [1] TRUE >> identical(structure(pi, a=1, b=2), structure(pi,a=1, aa=2)) > [1] TRUE > > so identical() is not even looking at the names of the attributes but only > checking their values. Oops ....Aha, so this is the problem then. Thank you for tracking down the problem! -- Lep pozdrav / With regards, Gregor Gorjanc ---------------------------------------------------------------------- University of Ljubljana PhD student Biotechnical Faculty Zootechnical Department URI: http://www.bfro.uni-lj.si/MR/ggorjan Groblje 3 mail: gregor.gorjanc <at> bfro.uni-lj.si SI-1230 Domzale tel: +386 (0)1 72 17 861 Slovenia, Europe fax: +386 (0)1 72 17 888 ---------------------------------------------------------------------- "One must learn by doing the thing; for though you think you know it, you have no certainty until you try." Sophocles ~ 450 B.C.