Carlos J. Gil Bellosta
2010-Oct-28 17:11 UTC
[Rd] Unexpected behabiour of min, tapply and POSIXct/POSIXlt classes?
Hello, I found rather surprising the behaviour of POSIXct and POSIXlt classes when combined with min and tapply. The details can be deduced from the script below: ############# Start of the script #################### before <- Sys.time() Sys.sleep( 1 ) now1 <- now2 <- Sys.time() my.times <- c( before, now1, now2 ) class( my.times ) ## [1] "POSIXct" "POSIXt" min( my.times ) ## [1] "2010-10-28 18:52:17 CEST" ### So far, so good... but: my.period <- c( "a", "b", "b" ) tapply( my.times, my.period, min ) ## a b ## 1288284737 1288284780 ## Where did my POSIXct class go? my.times.lt <- as.POSIXlt( my.times ) min( my.times.lt ) ## [1] "2010-10-28 18:52:17 CEST"; good! tapply( my.times.lt, my.period, min ) # $a # [1] 17.449 # # $b # [1] 52 # # Mensajes de aviso perdidos # In ansmat[index] <- ans : # n?mero de items para para sustituir no es un m?ltiplo de la longitud del reemplazo # # ?? :( ############# End of the script #################### Here are the details of my session (as reported by sessionInfo): R version 2.12.0 (2010-10-15) Platform: i386-pc-mingw32/i386 (32-bit) locale: [1] LC_COLLATE=Spanish_Spain.1252 LC_CTYPE=Spanish_Spain.1252 LC_MONETARY=Spanish_Spain.1252 LC_NUMERIC=C [5] LC_TIME=Spanish_Spain.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] RODBC_1.3-2 plotrix_3.0 loaded via a namespace (and not attached): [1] tools_2.12.0 I am running R on a Windows XP Box. Thank you very much, Carlos J. Gil Bellosta http://www.datanalytics.com
Joris Meys
2010-Oct-28 17:24 UTC
[Rd] Unexpected behabiour of min, tapply and POSIXct/POSIXlt classes?
Hi Carlos, this definitely does not belong to r-devel, just ask it on the normal help list or on www.stackoverflow.com next time. If you read the help file, this behaviour is exactly as expected : ---start quote--- Note that if the return value has a class (e.g. an object of class "Date") the class is discarded. ---end quote--- see ?tapply. So you do: tapply( my.times, my.period, min , simplify=F) There's your class. Cheers Joris On Thu, Oct 28, 2010 at 7:11 PM, Carlos J. Gil Bellosta <cgb at datanalytics.com> wrote:> Hello, > > I found rather surprising the behaviour of POSIXct and POSIXlt classes > when combined with min and tapply. > > The details can be deduced from the script below: > > ############# Start of the script #################### > > before <- Sys.time() > > Sys.sleep( 1 ) > > now1 <- now2 <- Sys.time() > > my.times <- c( before, ?now1, now2 ) > > class( my.times ) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ## [1] "POSIXct" "POSIXt" > > min( my.times ) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ## [1] "2010-10-28 18:52:17 CEST" > > ### So far, so good... but: > > my.period <- c( "a", "b", "b" ) > tapply( my.times, my.period, min ) > > ## ? ? ? ? ?a ? ? ? ? ?b > ## 1288284737 1288284780 > > ## Where did my POSIXct class go? > > > my.times.lt <- as.POSIXlt( my.times ) > > min( my.times.lt ) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?## [1] "2010-10-28 18:52:17 CEST"; good! > > tapply( my.times.lt, my.period, min ) > > # $a > # [1] 17.449 > # > # $b > # [1] 52 > # > # Mensajes de aviso perdidos > # In ansmat[index] <- ans : > # ? n?mero de items para para sustituir no es un m?ltiplo de la > longitud del reemplazo > # > # ?? ?:( > > > ############# End of the script #################### > > Here are the details of my session (as reported by sessionInfo): > > R version 2.12.0 (2010-10-15) > Platform: i386-pc-mingw32/i386 (32-bit) > > locale: > [1] LC_COLLATE=Spanish_Spain.1252 ?LC_CTYPE=Spanish_Spain.1252 > LC_MONETARY=Spanish_Spain.1252 LC_NUMERIC=C > [5] LC_TIME=Spanish_Spain.1252 > > attached base packages: > [1] stats ? ? graphics ?grDevices utils ? ? datasets ?methods ? base > > other attached packages: > [1] RODBC_1.3-2 plotrix_3.0 > > loaded via a namespace (and not attached): > [1] tools_2.12.0 > > I am running R on a Windows XP Box. > > > Thank you very much, > > Carlos J. Gil Bellosta > http://www.datanalytics.com > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >-- Joris Meys Statistical consultant Ghent University Faculty of Bioscience Engineering Department of Applied mathematics, biometrics and process control tel : +32 9 264 59 87 Joris.Meys at Ugent.be ------------------------------- Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php