Getting \n in column name

Am not able to get column name because of the \n

colnames(flavors_of_cacao)
[1] "Company \n(Maker-if known)"
[2] "Specific Bean Origin\nor Bar Name"
[3] "REF"
[4] "Review\nDate"
[5] "Cocoa\nPercent"
[6] "Company\nLocation"
[7] "Rating"
[8] "Bean\nType"
[9] "Broad Bean\nOrigin"

rename(company=company_maker_if_known)
Error in rename(company = company_maker_if_known) :
object 'company_maker_if_known' not found

rename(company=company\nmaker-if known)
Error: unexpected '\' in "rename(company=company"

At some point in its life, the data frame columns were renamed somehow to accommodate a data presentation table—that is, an attempt was made to make it look good. The work of data analysis benefits from being kept separate from the embellishments for presentation that can get in the way, as happened here.

The direct approach is to save the existing column names, replace them with a more useful set of names for analysis and then restore the original names when it comes time to decide on how to present results.

d <- data.frame(Company...Maker.if.known. = c(
  "A. Morin", "A. Morin",
  "A. Morin", "A. Morin", "A. Morin", "A. Morin"
), Specific.Bean.Origin.or.Bar.Name = c(
  "Agua Grande",
  "Kpime", "Atsane", "Akata", "Quilla", "Carenero"
), REF = c(
  1876L,
  1676L, 1676L, 1680L, 1704L, 1315L
), Review.Date = c(
  2016L, 2015L,
  2015L, 2015L, 2015L, 2014L
), Cocoa.Percent = c(
  "63%", "70%",
  "70%", "70%", "70%", "70%"
), Company.Location = c(
  "France", "France",
  "France", "France", "France", "France"
), Rating = c(
  3.75, 2.75,
  3, 3.5, 3.5, 2.75
), Bean.Type = c(
  " ", " ", " ", " ", " ",
  "Criollo"
), Broad.Bean.Origin = c(
  "Sao Tome", "Togo", "Togo",
  "Togo", "Peru", "Venezuela"
))

print_headers <- colnames(d)
colnames(d) <- c("maker","sp_orgin","ref","rev_date","pct_cocoa","location","rating","type","broad_origin")
head(d,2)
#>      maker    sp_orgin  ref rev_date pct_cocoa location rating type
#> 1 A. Morin Agua Grande 1876     2016       63%   France   3.75     
#> 2 A. Morin       Kpime 1676     2015       70%   France   2.75     
#>   broad_origin
#> 1     Sao Tome
#> 2         Togo
colnames(d) <- print_headers

Created on 2023-02-01 with reprex v2.0.2

Thank you.

Creating a new data frame might take a while being that the rows go up to 1796.
And I might mess some of the data while inputting it.

I was thinking of importing the data again. What could be the problem with this code;

library(readr)
flavors_of_cacao <- read_csv("flavors_of_cacao.csv")

Nothing at all, if that file has the column names without the \n; although I still think it will prove inconvenient working such long variable names.

Variable names are the same for each row and are set for the data frame as a whole with the colnames() function.

Thank you.

I did manage to rename the column names and am able to work with the data frame.

This topic was automatically closed 42 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.