当前位置: 动力学知识库 > 问答 > 编程问答 >

r - Pass object to plyr's rename()

问题描述:

Is it possible to pass variables to rename? For instance:

fake1 <- rename(mtcars, replace=c("mpg"="new.mpg"))

works just fine. However,

old_name <- "mpg"

fake2 <- rename(mtcars, replace=c(old_name="new.mpg"))

fails to map old_name to "mpg", instead gives the warning The followingfromvalues were not present inx: old_name.

Obviously I can rename using something like

names(mtcars)[names(mtcars)==old_name] <- "new.mpg"

but rename is so convenient (also I don't quite understand why the variable wouldn't be interpreted as the string it represents).

网友答案:

Try this instead:

rename(mtcars,replace = setNames("new.mpg",old_name))
分享给朋友:
您可能感兴趣的文章:
随机阅读: