Re: [Rd] POSIXlt matching bug

From: Sklyar, Oleg (London) <osklyar_at_ahl.com>
Date: Fri, 02 Jul 2010 08:27:05 +0100

POSIXlt is a list and it is not a list of dates or times, it is a list of

> x <- as.POSIXlt(Sys.Date())
> names(x)

[1] "sec" "min" "hour" "mday" "mon" "year" "wday" "yday" "isdst"

So if you want to match these things, you should use POSIXct or any other numeric-based format (as POSIXct is just a double value for the number of seconds since 1970-01-01) e.g.

> z <- as.POSIXct(Sys.Date())
> x <- as.POSIXct(Sys.Date())
> z==x

[1] TRUE
> match(z,x)

[1] 1
> z %in% x

[1] TRUE Dr Oleg Sklyar
Research Technologist
AHL / Man Investments Ltd
+44 (0)20 7144 3803
osklyar_at_maninvestments.com

> -----Original Message-----
> From: r-devel-bounces_at_r-project.org
> [mailto:r-devel-bounces_at_r-project.org] On Behalf Of McGehee, Robert
> Sent: 29 June 2010 15:46
> To: R-bugs_at_r-project.org; r-devel_at_r-project.org
> Subject: [Rd] POSIXlt matching bug
>
> I came across the below mis-feature/bug using match with
> POSIXlt objects
> (from strptime) in R 2.11.1 (though this appears to be an old issue).
>
> > x <- as.POSIXlt(Sys.Date())
> > table <- as.POSIXlt(Sys.Date()+0:5)
> > length(x)
> [1] 1
> > x %in% table # I expect TRUE
> [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
> > match(x, table) # I expect 1
> [1] NA NA NA NA NA NA NA NA NA
>
> This behavior seemed more plausible when the length of a
> POSIXlt object
> was 9 (back in the day), however since the length was redefined, the
> length of x no longer matches the length of the match function output,
> as specified by the ?match documentation: "A vector of the same length
> as 'x'".
>
> I would normally suggest that we add a POSIXlt method for match that
> converts x into POSIXct or character first. However, match does not
> appear to be generic. Below is a possible rewrite of match
> that appears
> to work as desired.
>
> match <- function(x, table, nomatch = NA_integer_,
> incomparables = NULL)
>
> .Internal(match(if(is.factor(x)||inherits(x, "POSIXlt"))
> as.character(x) else x,
> if(is.factor(table)||inherits(table, "POSIXlt"))
> as.character(table) else table,
> nomatch, incomparables))
>
> That said, I understand some people may be very sensitive to the speed
> of the match function, and may prefer a simple change to the ?match
> documentation noting this (odd) behavior for POSIXlt.
>
> Thanks, Robert
>
> R.version
> _
> platform x86_64-unknown-linux-gnu
> arch x86_64
> os linux-gnu
> system x86_64, linux-gnu
> status
> major 2
> minor 11.1
> year 2010
> month 05
> day 31
> svn rev 52157
> language R
> version.string R version 2.11.1 (2010-05-31)
>
> Robert McGehee, CFA
> Geode Capital Management, LLC
> One Post Office Square, 28th Floor | Boston, MA | 02109
> Tel: 617/392-8396 Fax:617/476-6389
> mailto:robert.mcgehee_at_geodecapital.com
>
>
> >This e-mail, and any attachments hereto, are intended for use by the
> addressee(s) only and may contain information that is (i) confidential
> information of Geode Capital Management, LLC and/or its affiliates,
> and/or (ii) proprietary information of Geode Capital Management, LLC
> and/or its affiliates. If you are not the intended recipient of this
> e-mail, or if you have otherwise received this e-mail in error, please
> immediately notify me by telephone (you may call collect), or
> by e-mail,
> and please permanently delete the original, any print outs and any
> copies of the foregoing. Any dissemination, distribution or copying of
> this e-mail is strictly prohibited.
>
> ______________________________________________
> R-devel_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>



 Please consider the environment before printing this email or its attachments. The contents of this email are for the named addressees ...{{dropped:19}}

R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Fri 02 Jul 2010 - 07:32:28 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 30 Sep 2010 - 18:30:43 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive