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

______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Reply via email to