22

Very simple, renaming colnames with dplyr gives me an odd error.

 library(dplyr)

 df <- data.frame(var1=c("one","two","three"),var2=c(1,2,3)) 

 df <- 
    df %>% 
    rename(var1=are.letters, var2=are.numbers)

Error: `are.letters`, `are.numbers` contains unknown variables

second try

 df <- rename(df, var1=are.letters, var2=are.numbers)

Error: `are.letters`, `are.numbers` contains unknown variables

Wondering if quoting....

df <- 
    df %>% 
    rename('var1'='are.letters', 'var2'='are.numbers')

Error: `are.letters`, `are.numbers` contains unknown variables
2
  • 11
    If you want to name the column names with 'are.eltters' and 'are.numbers, it should be the other way. Did you meant df %>% rename(are.letters = var1, are.numbers = var2) Commented Aug 24, 2017 at 9:48
  • 6
    @Akrun you're right. The order is just the opposite. Counter-intuitive IMHO Commented Aug 24, 2017 at 13:00

1 Answer 1

30

Just made the same error. So, I'm converting @akrun's useful comment into an "answer."

To fix this, the new name should be on the left hand of the equal sign and the existing name should be on the right.

Like this:

df <- df %>% 
    rename(are.letters = var1, are.numbers = var2)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.