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
Of course, if there is an easy fix to this that I am missing, please advise. Thanks!
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)