Message-ID: <98a30a7e-944f-4553-88b1-c2d9504d1771@fau.de>
Date: 2026-01-27T09:41:08Z
From: Sebastian Meyer
Subject: rep.int and rep_len do not drop names attribute
In-Reply-To: <5e00bdb8-23e7-4f7a-89c4-ebf2e2db79b6@sapo.pt>
This is a consequence of the following change in R 4.0.0 (r77905):
? When internal dispatch for ?rep.int()? and ?rep_len()?
fails, there is an attempt to dispatch on the equivalent
call to ?rep()?.
I believe it wasn't intended to change the handling of factors and I
think it would be desirable to restore the original (and still
documented) behavior of dropping names for rep.int(<factor>) and
rep_len(<factor>), so skip the (early) 'rep' fallback if `inherits(s,
"factor")`.
It would be good to report this in R's Bugzilla
<https://bugs.R-project.org/>, so there is a PR# for future reference.
At a first glance the patch looks simple (and should include a
regression test).
Sebastian Meyer
Am 25.01.26 um 11:32 schrieb Rui Barradas:
> Hello,
>
> In the documentation for ?rep, sections Value and Note say that
>
>
> > Value
> > An object of the same type as x.
>
> > rep.int and rep_len return no attributes (except the class if
> returning a factor).
>
> > The default method of rep gives the result names (which will almost
> always contain duplicates) if x had names, but retains no other attributes.
>
>
> > Note
> > Function rep.int is a simple case which was provided as a separate
> function partly for S compatibility and partly for speed (especially
> when names can be dropped). The performance of rep has been improved
> since, but rep.int is still at least twice as fast when x has names.
>
>
> And this works as documented.
>
>
> y <- 1:4
> names(y) <- letters[1:4]
> class(y) <- "new_class"
> attr(y, "this") <- "that"
>
> y
> #> a b c d
> #> 1 2 3 4
> #> attr(,"class")
> #> [1] "new_class"
> #> attr(,"this")
> #> [1] "that"
>
> rep(y, 2)????? # keeps names only
> #> a b c d a b c d
> #> 1 2 3 4 1 2 3 4
>
> rep.int(y, 2)? # no names and no other attributes
> #> [1] 1 2 3 4 1 2 3 4
>
> rep_len(y, 2)? # no names and no other attributes
> #> [1] 1 2
>
>
>
> But in the documentation's last example, the factor example, the names
> attribute is not dropped.
>
>
>
> ## named factor
> x <- factor(LETTERS[1:4]); names(x) <- letters[1:4]
> x
> #> a b c d
> #> A B C D
> #> Levels: A B C D
>
> rep(x, 2)
> #> a b c d a b c d
> #> A B C D A B C D
> #> Levels: A B C D
>
> rep(x, each = 2)
> #> a a b b c c d d
> #> A A B B C C D D
> #> Levels: A B C D
>
> rep.int(x, 2)? # no names? <--- Wrong comment
> #> a b c d a b c d
> #> A B C D A B C D
> #> Levels: A B C D
>
> rep_len(x, 10)
> #> a b c d a b c d a b
> #> A B C D A B C D A B
> #> Levels: A B C D
>
>
>
> Further test with attributes()
>
>
>
> rep.int(x, 2) |> attributes()
> #> $names
> #> [1] "a" "b" "c" "d" "a" "b" "c" "d"
> #>
> #> $class
> #> [1] "factor"
> #>
> #> $levels
> #> [1] "A" "B" "C" "D"
>
> rep_len(x, 10) |> attributes()
> #> $names
> #>? [1] "a" "b" "c" "d" "a" "b" "c" "d" "a" "b"
> #>
> #> $class
> #> [1] "factor"
> #>
> #> $levels
> #> [1] "A" "B" "C" "D"
>
>
>
> If I'm reading the docs right, this is wrong, isn't it?
>
>
> Hope this helps,
>
> Rui Barradas
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel