Search code examples
rdataframerename

Problem with columns in R... Column does not exist


I am newbie in programming languages, and I am learning R for data analysis. I was following along a course activity, and I'm having a hard time renaming the 'Company...Maker.if.known.' to 'Maker'. I tried to type in my RStudio desktop,

rename(maker = 'Company...Maker.if.known.')

it kept on saying that the column I was trying to rename did not exit. I already tried different variations of the column name that RStudio might recognize since even the 'Company...Maker.if.know.' RStudio doesn't recognize it. I installed all the packages and loaded them, for good measure, the tidyverse, ggplot2, and dplyr.

I'm not really sure what's wrong, or why the column is not being recognized, I believe the same error will occur for the rest of the columns. How can I solve this, or make the code work?

I will attach some screenshots or the the code I wrote and the error messages for context.

Thank you so much for taking the time to respond!

install.packages("tidyverse")
library(tidyverse)

install.packages("ggplot2")
library(ggplot2)

install.packages("dplyr")
library(dplyr)

flavors_df <- read_csv("flavors_of_cacao.csv")
str(flavors_df)
colnames(flavors_df)
head(flavors_df)

flavors_df %>% 
  rename(maker = `Company 
          (Maker-if known)`)

# i also ran the colnames() function, this is what I got

> colnames(flavors_df)
[1] "Company \n(Maker-if known)"        "Specific Bean Origin\nor Bar Name" "REF"                              
[4] "Review\nDate"                      "Cocoa\nPercent"                    "Company\nLocation"                
[7] "Rating"                            "Bean\nType"                        "Broad Bean\nOrigin"   


# some of the error messages I got trying to rename

> flavors_df %>% 
+   rename(maker = "Company \n(Maker-if known)")
Error in `rename()`:
! Can't rename columns that don't exist.
✖ Column `Company \n(Maker-if known)` doesn't exist.
Run `rlang::last_trace()` to see where the error occurred.
> View(flavors_df)
> flavors_df %>% 
+   rename(maker = "Company...Maker.if.know.")
Error in `rename()`:
! Can't rename columns that don't exist.
✖ Column `Company...Maker.if.know.` doesn't exist.
Run `rlang::last_trace()` to see where the error occurred.
> flavors_df %>% 
+   rename(maker = "Company (Maker-if known)")
Error in `rename()`:
! Can't rename columns that don't exist.
✖ Column `Company (Maker-if known)` doesn't exist.
Run `rlang::last_trace()` to see where the error occurred.
> flavors_df %>% 
+   rename(maker = "Company...Maker.if.known.")
Error in `rename()`:
! Can't rename columns that don't exist.
✖ Column `Company...Maker.if.known.` doesn't exist.
Run `rlang::last_trace()` to see where the error occurred.
> flavors_df %>% 
+   rename(maker = Company...Maker.if.known.)
Error in `rename()`:
! Can't rename columns that don't exist.
✖ Column `Company...Maker.if.known.` doesn't exist.
Run `rlang::last_trace()` to see where the error occurred.
> flavors_df %>% 
+   rename(maker = Company 
+          (Maker-if known))
Error: unexpected symbol in:
"  rename(maker = Company 
         (Maker-if known"
> flavors_df %>% 
+   rename(maker = `Company 
+           (Maker-if known)`)
Error in `rename()`:
! Can't rename columns that don't exist.
✖ Column `Company \n          (Maker-if known)` doesn't exist.
Run `rlang::last_trace()` to see where the error occurred.

I already tried different variations of the supposed column name to rename it, but it kept on giving the error messages. I am expecting to rename the 'Company...Maker.if.know.' to 'maker'.


Solution

  • The problem comes from read_csv creating that weird column name with the new line syntax \n.

    1. You can fix it by just using read.csv() instead. (This fixes Company \n(Maker-if known) to Company...Maker.if.known.)
    flavors_df <- read.csv("flavors_of_cacao.csv")
    
    flavors_df %>% 
      rename(maker = `Company...Maker.if.known.`)
    
    1. or you can use clean_names from the janitor package. (This fixes Company \n(Maker-if known) to company_maker_if_known)
    flavors_df <- read_csv("flavors_of_cacao.csv")
    flavors_df <- janitor::clean_names(flavors_df)
    
    flavors_df %>% 
      rename(maker = `company_maker_if_known`)