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.
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?dput(<your data frame>)into your post ordput(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.colnames(df2)[2] <- "distance". I have just renamed thedistcolumn in thecarsdataset todistance.