Error message in renaming columns

Hi everyone below is the code that I have in R Markdown which I'm having problems with:

daily_activity<- daily_activity %>% rename (ActivityDay= ActivityDate)
sleep_day<- sleep_day %>% rename (ActivityDay= SleepDay)

The error message that I'm getting is as followed:
Error in rename():
! Can't rename columns that don't exist.
:heavy_multiplication_x: Column ActivityDate doesn't exist.
Run rlang::last_trace() to see where the error occurred.

In my data frame there is a column named "Activity Date" as well as a column named "Sleep Day" and I'm trying to rename them. Can someone please tell me how to resolve the code error.

1 Like

"Activity Date" != ActivityDate
The space is the problem.

1 Like

I addressed the spacing in the code and it's still giving me the same error message.

The error occurs because R is case-sensitive and also doesn’t handle spaces in column names well. Try using backticks or check column names with colnames(daily_activity). Modify your code like this:

daily_activity <- daily_activity %>% rename(`ActivityDay` = `Activity Date`)  
sleep_day <- sleep_day %>% rename(`ActivityDay` = `Sleep Day`)  

If the column names contain hidden spaces or special characters, use names(daily_activity) to inspect them and clean them with janitor::clean_names().

It seems that my problems are stemming from the fact that I haven't loaded the packages into Rmd. When I went to load the appropriate packages in I'm getting the following message:

library(tidyr)
library(tidyverse)
library(purrr)
library(readr)
library(dbplyr)
library(magrittr)

── Attaching core tidyverse packages ──────────────────────────────────────── tidyverse 2.0.0 ──
:heavy_check_mark: dplyr 1.1.4 :heavy_check_mark: purrr 1.0.2
:heavy_check_mark: forcats 1.0.0 :heavy_check_mark: readr 2.1.5
:heavy_check_mark: ggplot2 3.5.1 :heavy_check_mark: stringr 1.5.1
:heavy_check_mark: lubridate 1.9.4 :heavy_check_mark: tibble 3.2.1
── Conflicts ────────────────────────────────────────────────────────── tidyverse_conflicts() ──
:heavy_multiplication_x: dplyr::filter() masks stats::filter()
:heavy_multiplication_x: dplyr::lag() masks stats::lag()
:information_source: Use the conflicted package to force all conflicts to become errors
Registered S3 methods overwritten by 'dbplyr':
method from
print.tbl_lazy
print.tbl_sql

Attaching package: β€˜dbplyr’

The following objects are masked from β€˜package:dplyr’:

ident, sql

Attaching package: β€˜magrittr’

The following object is masked from β€˜package:purrr’:

set_names

The following object is masked from β€˜package:tidyr’:

extract

WHAT DOES ALL THIS MEAN?

The messages inform you that there are functions with the same names in different packages and inform you which take precedence (due to the order of loading the packages).

1 Like

Thanks so much for your help!