1

I'm working on the capstone project for my Google Data Analytics certificate and I'm trying to rename the columns on 1 of my data sets to match the others in order to merge them, but I keep getting 2 different errors when attempting to execute it on just 1 column to start.

This is the code I've tried using and the old column name is exactly as it appears in the dataset (start_time being what I want to label it as and 01-Rental, etc as the current label):

Divvy_Trips_2019_Q2 %>%  
  rename(start_time=01 - Rental Details Local Start Time)

And I get:

Error: unexpected symbol in:
"Divvy_Trips_2019_Q2 %>%
rename(start_time=01 - Rental Details"

As well as a red x next to that line of code that indicates:

unexpected token 'Details'
unexpected token 'Start

I found a related article that suggested to add backticks to the column name with all the spaces, but this didn't work either. I get another error when attempting that fix:

Divvy_Trips_2019_Q2 %>%   
  rename(start_time=`01 - Rental Details Local Start Time`)

Error: unexpected symbol in:
"Divvy_Trips_2019_Q2 %>%
rename(start_time=01 - Rental Details"

I've attempted a colnames code that also didn't work:

colnames(Divvy_Trips_2019_Q2$`01 - Rental Details Local Start Time`[2]) <- 'start_time'

Error in colnames<-(*tmp*, value = "start_time") :
attempt to set 'colnames' on an object with less than two dimensions

When trying to research the original error, I couldn't find much else in terms of suggestions given the remaining articles seemed to be too old or more complex than what I'm trying to do.

I'm also interested in how I can do this with multiple columns at a time.

4
  • 1
    library(dplyr); df <- tibble(`01 - Rental Details Local Start Time` = 1); df %>% rename(start_time=`01 - Rental Details Local Start Time`) works for me. What do you get when you run this code? Commented Aug 7, 2024 at 16:30
  • 1
    Please read about how to make a great R reproducible example and update your question accordingly. Include a sample of your data by pasting the output of dput(<your data frame>) into your post or dput(head(<your data frame>)) if you have a large data frame. If you cannot post your data, then post code for creating representative data. You can get more exact help if you post a reproducible example. Commented Aug 7, 2024 at 16:38
  • This is how to rename a column very simply using base R: colnames(df2)[2] <- "distance". I have just renamed the dist column in the cars dataset to distance. Commented Aug 7, 2024 at 16:57
  • Thank you both! @IfeanyiIdiaye that worked, I truly appreciate it!! Commented Aug 8, 2024 at 1:13

0

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.