2

I am trying top rename variables within the flow of work. This problem occurs when I want to rename variables programmatically, and don't want to break the flow and perform an names(df)<-namevect operation.

see below for a minimal example that throws

Error: All arguments to rename() must be named.

testdf <- mtcars %>% 
  rename_(.dots=make.names(1:11)) %>% 
  mutate(X3=X3*2)
Sotos
  • 51,121
  • 6
  • 32
  • 66
Jonno Bourne
  • 1,931
  • 1
  • 22
  • 45
  • 7
    use `setNames`, it makes it easier in a pipe, i.e. `mtcars %>% setNames(make.names(1:11)) %>% ...` – talat Sep 08 '16 at 14:09
  • How about [this tread](http://stackoverflow.com/questions/37526921/loop-rename-variables-in-r-with-assign)? – 989 Sep 08 '16 at 14:12
  • For the special case of renaming all variables with `X1, X2, ..., Xn` you can also use `select(mtcars, X = everything())` – talat Sep 08 '16 at 14:17
  • This link also has some useful info: http://stackoverflow.com/questions/21502465/replacement-for-rename-in-dplyr – G. Grothendieck Sep 08 '16 at 14:50
  • Thanks for the tip I changed to setNames. It's weird that it is such a struggle to use when everything else in dplyr is so smooth. – Jonno Bourne Sep 09 '16 at 09:45

0 Answers0