3

I have a CSV file containing some columns with names like "beauty & spas", "american (new)" etc. When I read this file in R and use names() to see column names, they have been converted to "beauty...spas.1" and "american...new..1". How do I prevent them from being converted? I do not want to correct them manually.

Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418

1 Answers1

14

If you read the documentation carefully at ?read.table (or ?read.csv) you will quickly see that there is an argument called check.names. You most likely want to set that to FALSE. Keep in mind, though, that those are not syntactically valid column names in R, so you you might actually prefer to change them to something that R will handle more smoothly anyway.

joran
  • 169,992
  • 32
  • 429
  • 468
  • Thank you for replying instantly. Great help. –  Jun 09 '13 at 13:39
  • 1
    What joran is saying is that you can do `data$thing` to get a column but you have to quote it if it has a 'syntactically invalid' name: `data$"beauty & spas"` which is a bit awkward... Note you can always do something like `read.csv("test.csv",nrows=1, head=FALSE)` to just read the first line of a file to get the column headers. – Spacedman Jun 09 '13 at 15:19
  • 1
    (Also, if Joran has answered your question, click the little tick mark!) – Spacedman Jun 09 '13 at 15:20