dplyr 'rename' standard evaluation function not working as expected?

前端 未结 2 803
终归单人心
终归单人心 2020-12-13 15:53

UPDATE: From comment below on this post, this is now working as expected, without the issues I laid out here.

Below is a toy example of using rename_ fr

相关标签:
2条回答
  • 2020-12-13 16:09

    There are a few thing that make this painful:

    1. c(x = "new") is the same as c("x" = "new"), and not the opposite of c(new = x).

    2. You can construct the vector you want with setNames(x, "new"), but...

    3. I forgot to add the .dots argument to rename_ (bug report at https://github.com/hadley/dplyr/issues/708) so you can't do:

      rename_(dat, .dots = setNames(x, "new"))
      

      Instead you need to use do.call:

      do.call(rename_, c(list(quote(dat)), list(setNames(x, "new"))))
      
    0 讨论(0)
  • 2020-12-13 16:25

    In my Rstudio, I define:

    x <- 'myname'
    dat <- data.frame(yes=1, no=2)
    

    I want to change the variable name from yes to myname

    It doesn't work:

    rename_(dat, .dots = setNames(x, "yes"))
    

    But this works:

    rename_(dat, .dots = setNames("yes", x))
    
    rename_(dat, .dots = setNames("yes", paste(x) ))
    
    0 讨论(0)
提交回复
热议问题