-2

The rename function from dplyr is not working on my system.

I'm running R 3.6.1, dplyr 0.8.3, rlang 0.4.0, on Rstudio 1.2.1335 and I can't get rename to work.

test <- data.frame(X1 = c(2, 3, 4), X2 = c(5, 6, 7))

test <- test %>% 
  rename(X1 = column1)

I expect the variable name X1 to change to column1. What I am getting is:

Error in .f(.x[[i]], ...) : object 'column1' not found
  • 1
    I often forget the order for this. As a helpful memory technique, think of `rename` as just like `mutate` except with an extra "remove old column" step. That helps me remember, since I know the order I would use in `mutate` already. (of course, you can also refer to `help(rename)`) – IceCreamToucan Sep 04 '19 at 14:37
  • 1
    Says it in the `dplyr::rename` docs: "Use named arguments, e.g. new_name = old_name, to rename selected variables." – camille Sep 04 '19 at 14:48
  • I'm a dumbass! Thanks everyone! I kept looking at Hadley's R4DS and missed that the new variable name must come first. My bad! Thank you again! – goncaloveiga Sep 04 '19 at 15:01
  • 1
    @akrun thanks, feel free to add it as target – Cath Sep 04 '19 at 16:29

1 Answers1

0

We need to specify it in the reverse

library(dplyr)
test %>% 
  rename(column1 = X1)
#  column1 X2
#1       2  5
#2       3  6
#3       4  7
akrun
  • 874,273
  • 37
  • 540
  • 662