Error in rename of column names in R

q2_2019 %>%
           rename( trip_id =  01-Rental Details Rental ID
                   , start_time=  01-Rental Details Local Start Time
                   ,end_time= 01- Rental Details Local End Time
                   ,bikeid=  01-Rental Details Bike ID
                   ,tripduration= 01- Rental Details Duration In Seconds Uncapped
                   ,from_station_id= 03- Rental Start Station ID
                   ,from_station_name= 03-Rental Start Station Name
                   ,to_station_id= 02-Rental End Station ID
                   ,to_station_name= 02 - Rental End Station Name 
                   ,usertype= User Type
                   ,gender= Member Gender
                   ,birthyear= 05-Member Details Member Birthday Year)

Those are non syntactic variable names, they need to be enclosed between backticks.

trip_id = `01-Rental Details Rental ID`
1 Like

Hi, I tried doing it with backticks but it still doesn't work.
Thanks (I am a newbie)
Error in chr_as_locations():
! Can't rename columns that don't exist.
:heavy_multiplication_x: Column 01-Rental Details Rental ID doesn't exist

According to the documentation, dplyr::rename() has a "new name" = "old name" syntax (This is what your error indicates, since your "new name" is in the wrong place and dplyr tries to rename a non existing column). Hence, you would need to do

data |> rename(`01-Rental Details Rental ID` = trip_id)

Kind regards

1 Like

Thanks for replying.
I have put new name = old name but it still doesn't work.

When you wish to be supported with something that doesn't work; its recommended that you be as descriptive as possible to describe how or in what way it "doesnt work"; does it not work, because

  • it gives things the wrong names ?
  • the names stay as they were ?
  • an error is thrown ?
    • if an error is thrown what is the message of the error ?
1 Like

Here is an example with your column trip_id and how it is renamed using dplyr::rename():

Data <- data.frame(
  trip_id = 1:3
)

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

Data |>
  rename(
    `01-Rental Details Rental ID` = trip_id
  )
#>   01-Rental Details Rental ID
#> 1                           1
#> 2                           2
#> 3                           3

Created on 2022-09-28 with reprex v2.0.2

If you installed the actual version of dplyr and use the current R version (4.2) - this should work as intended. Did you forget to use ``?

Kind regards

1 Like
> library(dplyr)
> q2_2019 <- rename( trip_id =  `01-Rental Details Rental ID`
+                                   , start_time= `01-Rental Details Local Start Time`
+                                   ,end_time = `01- Rental Details Local End Time`
+                                   ,bikeid =  `01-Rental Details Bike ID`
+                                   ,tripduration= `01- Rental Details Duration In Seconds Uncapped`
+                                   ,from_station_id= `03- Rental Start Station ID`
+                                   ,from_station_name= `03-Rental Start Station Name`
+                                   ,to_station_id = `02-Rental End Station ID`
+                                   ,to_station_name = `02 - Rental End Station Name`
+                                   ,usertype = `User Type`
+                                   ,gender=  `Member Gender`
+                                   ,birthyear = `05-Member Details Member Birthday Year`)
Error in rename(trip_id = `01-Rental Details Rental ID`, start_time = `01-Rental Details Local Start Time`,  : 
  unused arguments (trip_id = `01-Rental Details Rental ID`, start_time = `01-Rental Details Local Start Time`, end_time = `01- Rental Details Local End Time`, bikeid = `01-Rental Details Bike ID`, tripduration = `01- Rental Details Duration In Seconds Uncapped`, from_station_id = `03- Rental Start Station ID`, from_station_name = `03-Rental Start Station Name`, to_station_id = `02-Rental End Station ID`, to_station_name = `02 - Rental End Station Name`, usertype = `User Type`, gender = `Member Gender`, birthyear = `05-Member Details Member Birthday Year`)

Here sou used old_name = new_name again. This will not work..

1 Like

In this case theres no data.frame involved in the rename

new name= trip_id
old name = 01-Rental details Local Start Time

FactOREO was explaining to you that the syntax of the rename() function is that where you want to rename and have some name that could be consider the "old name" to a name that we shall call for our purposes "new name"; the "new name" should be put first before the equal sign and the "old name" should be put second after the equal sign.

if your new name is trip_id then it should be first before the equal sign; "01 - Rental etc" should be 2nd , i.e. to the right of the equal sign.

1 Like

Done like that.
Thank you

If your issue is solved, please consider accepting one of the answers which solved your issue. This way other users know that there was a solution found for your specific request. :slight_smile:

No, my issue is not solved.
Thank you for replying FactOREO

Well, then you should provide some further information including a reprex. But I am a bit puzzled why it shouldn't work now with the solutions given. Maybe you can copy and paste your code after adjustments which were given from the other users. I suspect you have a typo somewhere which causes the error.

Can you provide a reproducible example of your dataset? It will help if people can see what your dataset looks like, especially with your issue here.

hi bro,
Try this code:

names(q2_2019) <- c('trip_id, 'start_time', 'end_time', 'tripduration, 'from_station_id', 'from_station_name', 'to_station_id, 'to_station_name', 'usertype', 'gender','birthyear')

good luck bro

If you want to give the names I see in your question (with that order), you can use the colnames function.

c1 = c("01-Rental Details Rental ID",
"01-Rental Details Local Start Time",
"01- Rental Details Local End Time",
"01-Rental Details Bike ID",
"01- Rental Details Duration In Seconds Uncapped",
"03- Rental Start Station ID",
"03-Rental Start Station Name",
"02-Rental End Station ID",
"02 - Rental End Station Name",
"User Type","Member Gender",
"05-Member Details Member Birthday Year") #a vector which has all the column names

colnames(q2_2019) = c1

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.