1

In my dataframe I have column names with dashes and spaces e.g. address-type. When I try to export as .csv, the dashes and spaces change into dots, so "address-type" changes into address.type in the .csv file.

When importing .csvs I used check.names = FALSE, however, there doesn't seem to be a similar function when exporting.

Any help on this annoying problem would be greatly appriciated!

Edits to the question with example code:

df1 = data.frame("x 1" = 1, "x-2" = 2, check.names = F)
df2 = data.frame("y 1" = 1, "y-2" = 2, check.names = F)

Dflist <- list(df1, df2)
write.csv(Dflist[1],file="test.csv")
  • I can't reproduce the problem. If I define `df = data.frame("x 1" = 1, "x-2" = 2, check.names = F)` and then do `write.csv(df, file = "test.csv")`, the first row of the file is `"","x 1","x-2"`, which looks just fine. – Gregor Thomas May 30 '19 at 13:05
  • Hmm that is really wierd. I tried your code, and as you mentioned the dashes and gaps are intact. I'll have to investigate further to see, whether the problem is caused due to something else. Nonetheless, thanks for the prompt reply! – Abir Hossain May 30 '19 at 13:15
  • Hi Gregor, I was able to reproduce the problem. I've edited the question now with example code. Would be great if you could look at it. – Abir Hossain May 30 '19 at 13:21
  • 1
    `Dflist[1]` in the last line should be `Dflist[[1]]`. `Dflist[1]` is not a data frame, it is a one component list containing a data.frame, but `Dflist[[1]]` is a data frame. – G. Grothendieck May 30 '19 at 13:48
  • 2
    See R-FAQ [The difference between [ and [[ in list indexing](https://stackoverflow.com/q/1169456/903061), also https://twitter.com/hadleywickham/status/643381054758363136?lang=en – Gregor Thomas May 30 '19 at 13:50

1 Answers1

0

Use write_csv insterad of write.csv from the tidyverse ecosystem, which is part of the readr library.

It does exactly what you want.

jens_laufer
  • 190
  • 3
  • 13