[R] cut POSIX results in NA - bug?
Gabor Grothendieck
ggrothendieck at myway.com
Wed Nov 3 15:53:38 CET 2004
Petr Pikal <petr.pikal <at> precheza.cz> writes:
:
: 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.
:
: 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. If yes I can count with it in improvement of
: my function(s), if not I can make some temporary workaround.
:
Your question has already been answered but here is an alternate
approach that avoids cut. We format the datetimes and truncate each
string at the hour, make that a factor and then get the integer codes:
R> cutHour <- function(x) as.integer(factor(substring(format(x),1,13)))
R> cutHour(datum[1370:1381])
[1] 1 1 1 1 1 1 1 1 1 1 1 2
R> cutHour(datum[1370:1382])
[1] 1 1 1 1 1 1 1 1 1 1 1 2 2
More information about the R-help
mailing list