[R] strange behaviour of "POSIXlt" "POSIXt" object

William Dunlap wdunlap at tibco.com
Mon Feb 27 17:25:26 CET 2012


is.na(POSIXltObject) can behave oddly if you manipulate
the fields of the POSIXlt object directly so as to
cause illegal combinations of values.  E.g., the 35th
of February, 2009, is not considered an NA but the 36th
and above are:

  > z <- as.POSIXlt("2009-02-25 06:30:00")
  > for(i in 1:20){ z$mday <- z$mday + 1L ; cat(is.na(z), ": ");print(z)}
  FALSE : [1] "2009-02-26 06:30:00"
  FALSE : [1] "2009-02-27 06:30:00"
  FALSE : [1] "2009-02-28 06:30:00"
  FALSE : [1] "2009-03-01 06:30:00"
  FALSE : [1] "2009-03-02 06:30:00"
  FALSE : [1] "2009-03-03 06:30:00"
  FALSE : [1] "2009-03-04 06:30:00"
  FALSE : [1] "2009-03-05 06:30:00"
  FALSE : [1] "2009-03-06 06:30:00"
  FALSE : [1] "2009-03-07 06:30:00"
  TRUE : [1] "2009-03-08 06:30:00"
  TRUE : [1] "2009-03-09 06:30:00"
  TRUE : [1] "2009-03-10 06:30:00"
  TRUE : [1] "2009-03-11 06:30:00"
  TRUE : [1] "2009-03-12 06:30:00"
  TRUE : [1] "2009-03-13 06:30:00"
  TRUE : [1] "2009-03-14 06:30:00"
  TRUE : [1] "2009-03-15 06:30:00"
  TRUE : [1] "2009-03-16 06:30:00"
  TRUE : [1] "2009-03-17 06:30:00"

The print routine does not seem to consult is.na().

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of ikuzar
> Sent: Monday, February 27, 2012 2:41 AM
> To: r-help at r-project.org
> Subject: Re: [R] strange behaviour of "POSIXlt" "POSIXt" object
> 
> Hi,
> 
> I do not know what part of my code should I post here (I use large size of
> data, the loop "for" contains many lines). The situation is difficult to
> post here, but I hope these lines would be useful for help:
> 
> hcEndDateTmp = userDateStart
>   if((hcStartTime<="23:59") & (hcEndTime >= "00:00")){
>     hcEndDateTmp$mday = userDateStart$mday + 1
>   }
>   hcEndDate = strftime(hcEndDateTmp, "%Y-%m-%d")
>   hcStart = ""
> hcEnd = as.POSIXlt(paste(hcEndDate, hcEndTime))
> 
> for(rowNum in 1:nbJour){
>     ...
>     hcStart = as.POSIXlt(ecs$startAt[[rowNum]])
>     ecsInterval2 =
> datePower[(datePower$DateTime>=hcStart)&(datePower$DateTime<=hcEnd), ]
>     ...
>    hcEnd$mday = hcEnd$mday + 1
>   cat("DEBUG: rowNum=", rowNum, "is.na(hcEnd)=", is.na(hcEnd), "\n")
> }
> 
> the two last lines are very important. It yields:
> 
> DEBUG: rowNum= 1 is.na(hcEnd)= FALSE
> DEBUG: rowNum= 2 is.na(hcEnd)= FALSE
> DEBUG: rowNum= 3 is.na(hcEnd)= FALSE
> DEBUG: rowNum= 4 is.na(hcEnd)= FALSE
> DEBUG: rowNum= 5 is.na(hcEnd)= FALSE
> DEBUG: rowNum= 6 is.na(hcEnd)= FALSE
> DEBUG: rowNum= 7 is.na(hcEnd)= TRUE
> 
> Browse[1]> hcEnd
> [1] "2009-03-29 06:30:00"
> Browse[1]> class(hcEnd)
> [1] "POSIXlt" "POSIXt"
> 
> So, At the end of 7th iteration, hcEnd becomes NA (I guess because of
> hcEnd$mday = hcEnd$mday + 1 ?!?). But It remains strange because the class
> of hcEnd is here "POSIXlt" "POSIXt" (it should be "logical".!! Am I
> wrong???)
> 
> Here is the version of R:
> platform       i386-pc-mingw32
> arch           i386
> os             mingw32
> system         i386, mingw32
> status
> major          2
> minor          13.2
> year           2011
> month          09
> day            30
> svn rev        57111
> language       R
> version.string R version 2.13.2 (2011-09-30)
> 
> thanks for your help
> 
> --
> View this message in context: http://r.789695.n4.nabble.com/strange-behaviour-of-POSIXlt-POSIXt-
> object-tp4418115p4424214.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list