I want to apply new column names from a vector to a dataframe.
There are already good answers to this question:
Applying dplyr's rename to all columns while using pipe operator, How do I add a prefix to several variable names using dplyr?
My question is explicitly:
What is the equivalent of rename_with to this:
newcolnames <- c("one", "two", "three", "four", "five") head(iris) %>% setNames(newcolnames) head(iris) %>% `colnames<-`(newcolnames)
head(iris) %>% rename_with(iris, newcolnames)
I want to use explicitly
It doesn’t seem like
rename_with is the right function for this. I guess you could do
iris %>% rename_with(~newcolnames)
as kind of a hack. You can also use
iris %>% rename(!!!setNames(names(.), newcolnames))
setNames method just seems much more apt.