rename_with.data.frame() has unexpected results when .fn returns a factor -- or at least unexpected for me. Documentation does suggest that .fn should return a character vector, but I wouldn't expect factors to be coerced to integers first.
The coercion occurs within [<-, which could be replaced with vec_slice()?
|
names[cols] <- .fn(names[cols], ...) |
df <- data.frame(
a = 1,
b = 2,
c = 3
)
foo <- function(x) {
factor(toupper(x))
}
dplyr::rename_with(df, foo)
#> 1 2 3
#> 1 1 2 3
# name reassignment within rename_with.data.frame()
x <- c("a", "b", "c")
x[1:3] <- foo(x)
x
#> [1] "1" "2" "3"
# vctrs seems fine
x <- c("a", "b", "c")
vctrs::vec_slice(x, 2:3) <- foo(x[2:3])
x
#> [1] "a" "B" "C"
Created on 2022-11-23 with reprex v2.0.2
rename_with.data.frame()has unexpected results when.fnreturns afactor-- or at least unexpected for me. Documentation does suggest that.fnshould return acharactervector, but I wouldn't expect factors to be coerced to integers first.The coercion occurs within
[<-, which could be replaced withvec_slice()?dplyr/R/rename.R
Line 69 in 434caa2
Created on 2022-11-23 with reprex v2.0.2