0

I am teaching myself R, and just came across a renaming problem in tidyverse. After some research, I assumed my question had already been answered before:

How to rename a column to a variable name "in a tidyverse way"

as my goal was to rename columns of a tibble (called working) previously read from an Excel file:

# A tibble: 9 x 5
X__1       X__2        X__3   X__4   X__5
<date>     <date>     <dbl>  <dbl>  <dbl>
1 2018-01-01 2018-01-06 34.0    2.00   2.00
2 2018-01-07 2018-01-13 33.5    1.50   3.50
3 2018-01-14 2018-01-20 33.8    1.75   5.25
4 2018-01-21 2018-01-27 40.5    8.50  13.8 
5 2018-01-28 2018-02-03 39.2    7.25  21.0 
6 2018-02-04 2018-02-10 30.5  - 1.50  19.5 
7 2018-02-11 2018-02-17 33.2    1.25  20.8 
8 2018-02-18 2018-02-24 35.2    3.17  23.9 
9 2018-02-25 2018-03-03  4.00 -28.0  - 4.08

However, when trying to use the suggested code from the answer in the previous link:

rename(working, !!startdate := X__1)

I got the following error message:

Error in (function (x)  : object 'startdate' not found

Now I am confused as my assumption was that startdate was the name I was assigning as a new name, as the syntax should be "new_name=old_name"? I checked the dplyr version, so I don't think this is the problem:

packageVersion("dplyr")
[1] ‘0.7.4’

Does anyone know what is causing the problem? Please note that I am aware of the option to assign the column names via

colnames(working)[1] <- "startdate"

but the idea was really to do this via tidyverse.

NelnewR
  • 131
  • 7
  • 1
    in your case it would only be `rename(working, startdate = X__1)`, the question you link to is different because the new column name is stored in a variable – moodymudskipper Feb 26 '18 at 12:52
  • Could you explain why? I would like to understand the difference to the example given before? – NelnewR Feb 26 '18 at 12:55
  • 2
    You just need to add this, before the `rename` line: `startdate <- "New ColName"` – Hackerman Feb 26 '18 at 12:55

0 Answers0