User247365
User247365

Reputation: 685

dplyr rename command with spaces

I have tried multiple variations of the rename function in dplyr.

I have a data frame called alldata, and a column within the data frame named WindDirection:N. I am trying to rename it to Wind Direction. I understand creating variable names containing spaces is not a good practice, but I want it to be named as such to improve readability for a selectInput list in shiny, and even if I try to rename it to WindDirection I get the same error messages.

I have tried:

rename(alldata, Wind Direction = WindDirection:N)

which gives the error message:

Error: unexpected symbol in "rename(alldata, Wind Direction"

rename(alldata, `Wind Direction` = `WindDirection:N`)

which does not give an error message, but also does not rename the variable

rename(alldata, "Wind Direction" = "WindDirection:N")

which gives the error message:

Error: Arguments to rename must be unquoted variable names. Arguments Wind Direction are not.

I then tried the same 3 combinations swapping the variable names, that is, putting the old variable first and the new variable second, obtaining similar error messages.

I then tried to specify the package as below and tried all 6 combinations again.

dplyr::rename(alldata, `Wind Direction` = `WindDirection:N`)

obtaining similar error messages as the first time.

I have used the following thread as a reference.

Upvotes: 4

Views: 5680

Answers (1)

User247365
User247365

Reputation: 685

as agenis pointed out, my mistake was not redefining the dataframe after renaming the variable.

So where I had

dplyr::rename(alldata, `Wind Direction` = `WindDirection:N`)

I should have

alldata <- dplyr::rename(alldata, `Wind Direction` = `WindDirection:N`)

Upvotes: 2

Related Questions