我想將向量中的新列名應用于資料幀。
這個問題已經有了很好的答案: Applying dplyr's rename to all columns while using pipe operator , How do I add a prefix to幾個變數名 using dplyr?
我的問題是明確的:rename_with 的等價物是什么:
newcolnames <- c("one", "two", "three", "four", "five")
head(iris) %>%
setNames(newcolnames)
head(iris) %>%
`colnames<-`(newcolnames)
我試過:
head(iris) %>%
rename_with(iris, newcolnames)
我想明確使用rename_with!
uj5u.com熱心網友回復:
這似乎不是rename_with正確的功能。我想你可以做到
iris %>%
rename_with(~newcolnames)
作為一種黑客。你也可以使用rename
iris %>%
rename(!!!setNames(names(.), newcolnames))
但這種setNames方法似乎更貼切。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/351287.html
下一篇:將多行合并為每組一行
