I have a dataframe with columns id, feature_1, feature_2, feature_3 as follows.
df = data.frame(
id = sample(letters, 5),
feature_1 = sample(1:10, 5),
feature_2 = runif(5),
feature_3 = rnorm(5)
)
I want to rename all the feature columns by adding a prefix. The following line doesn't work and output error.
df %>%
rename_with(~(ifelse(names(.x) == "id", paste0("source_", names(.x)), "id")))
Error in names[cols] <- .fn(names[cols], ...) :
replacement has length zero
Any hint on how to modify this? What does .x represent inside rename_with? Thanks in advance!