[Rd] Setting of "balanced" attribute by 'length<-.POSIXlt'

Suharto Anggono Suharto Anggono @uh@rto_@nggono @end|ng |rom y@hoo@com
Fri Dec 26 22:14:09 CET 2025


 I just realized that the default method of 'length<-' in R allowed a string as the assigned length.

When 'value' is a string, each of
value - length(x) < 1L
and
trunc(value) <= length(x)
give an error, while
value < length(x) + 1
uses string comparison.

-----------
On Saturday, 13 December 2025 at 08:02:28 pm GMT+7, Suharto Anggono Suharto Anggono <suharto_anggono using yahoo.com> wrote:


In R 4.5.2 (not before), `length<-.POSIXlt` sets "balanced" attribute of the result to TRUE only if
isTRUE(attr(x, "balanced")) && value <= length(x) .

For 'value' that is not a whole number, it is possible that value > length(x) and the length of the result is still length(x).

Instead of
value <= length(x) ,
it is better to use
value < length(x) + 1
(not length(x) + 1L as length(x) may be .Machine$integer.max)
or
value - length(x) < 1L
or
trunc(value) <= length(x)
or
length(r) <= length(x)
or
length(r$year) <= length(x)
(or other component of 'r').

By the way, `length<-.POSIXlt` should also use unCfillPOSIXlt(x) instead of unclass(x). For example, when the maximum component length is 3 and a component has length 1 and 'value' (the assigned length) is 2.

If unCfillPOSIXlt(x) is used and is assigned to 'x', length of any component can be used.  
	[[alternative HTML version deleted]]



More information about the R-devel mailing list