Rename in dplyr 0.7+ function

后端 未结 2 898
无人共我
无人共我 2020-12-19 13:03

I am having difficulty renaming columns in a function using dplyr. I have already found helpful posts on nonstandard evaluation and the use of enquo (e.g., http://dplyr.tidy

相关标签:
2条回答
  • 2020-12-19 13:51

    To rename multiple columns at once, you have to use !!!:

    x <- c("mpg", "cyl")
    xnew <- paste(x, 2, sep = ".")
    names(x) <- xnew
    rename(mtcars, !!!x)
    
    0 讨论(0)
  • 2020-12-19 14:07

    Use := if you want programmatically-assigned names on the LHS of expressions.

    x <- "mpg"
    xnew <- "mpg2"
    rename(mtcars, !!xnew := !!rlang::sym(x))
    
    0 讨论(0)
提交回复
热议问题