rxlsxr-xlsx

Why does a question mark in a dataframe column title show as a period when outputting from r to an excel sheet (using xlsx package)?


Suppose I use the xlsx package to write a dataframe from r to excel. And suppose I have a dataframe with a column title that includes a question mark (code below):

library(xlsx)
rm(list = ls())

fpath <- "whatever filepath"

#My df
df <- data.frame("Questions?" = c("bla", "bla bla"), "123Numbers"= c(1,2))

#Now output 
wb <- createWorkbook()
sh <- createSheet(wb, sheetName = "Bla")
addDataFrame(df, sh)

saveWorkbook(wb, paste0(fpath,".xlsx"))

When I open the excel output file, I get the following: Result

  1. Why does the question mark show up as a period in the Questions column title?
  2. Why is there a random "X" character in front of the 123Numbers column title?

Of course, if there is an easy fix to this that I am missing, please advise. Thanks!


Solution

  • The check.names argument is by default TRUE in data.frame and it would make sure that all the non-standard column names are modified by applied (make.names-> make.unique). If we specify it as FALSE, the column names would not get changed

    df <- data.frame("Questions?" = c("bla", "bla bla"),
        "123Numbers"= c(1,2), check.names = FALSE)