[R] cut POSIX results in NA - bug?

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Nov 3 12:20:57 CET 2004


On Wed, 3 Nov 2004, Petr Pikal wrote:

> Dear all
> 
> I try to make hourly average by cut() function, which almost works 
> as *I* expected. What puzled me is that if there is only one item at 
> the end of your data it results in NA.
> 
> Example will explain what I mean
> 
> datum<-seq(ISOdate(2004,8,31), ISOdate(2004,9,1), "min")
> 
> cut(datum[1370:1381],"hour", labels=F)
>  [1]  1  1  1  1  1  1  1  1  1  1  1 NA
> 
> cut(datum[1370:1382],"hour", labels=F)
>  [1] 1 1 1 1 1 1 1 1 1 1 1 2 2
> 
> I do not understand why the last item in first call is NA. I found it 
> only when there was a switch from DST to standard time as it 
> coused a trouble in one of my functions and I found there is NA 
> value where I did not expected it. 

cut(datum[1370:1381],"hour", labels=F, include.lowest=T)

is what you need.  See ?cut, in the See Also, which says

include.lowest: logical, indicating if an 'x[i]' equal to the lowest
          (or highest, for 'right = FALSE') 'breaks' value should be
          included.

> I can make some workaround but can you please explain me why 
> first call results in NA value at the end of a vector and if it is 
> *intended* behaviour. 

It is the documented behaviour, for better or for worse.

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




More information about the R-help mailing list