有人应该已经问过这个问题了,但我找不到答案。就说我已经问过了:
x = data.frame(q=1,w=2,e=3, ...and many many columns...)
what is the most elegant way to rename an arbitrary subset of columns, whose position I don't necessarily know, into some other arbitrary names?
例如,假设我想把 "q"
和 "e"
重命名为 "A"
和 "B"
,那么最优雅的代码是什么呢?
显然,我可以做一个循环:
oldnames = c("q","e")
newnames = c("A","B")
for(i in 1:2) names(x)[names(x) == oldnames[i]] = newnames[i]
但是我想知道是否有一个更好的方法? 也许使用一些软件包? (plyr::rename
等)