58

I would like to store the contents of my data frame into a .csv file without the names of columns. I use the following piece of code,

write.csv(cur_data,new_file, row.names = F, col.names=F)

The resulting file looks like this,

"V1","V2"
-0.02868862,5.442283e-11
-0.03359281,7.669754e-12
-0.03801883,-1.497323e-10
-0.04320051,-6.557672e-11

However I would like to have the file in the following format,

-0.02868862,5.442283e-11
-0.03359281,7.669754e-12
-0.03801883,-1.497323e-10
-0.04320051,-6.557672e-11

I don't understand why the the col.names parameter in the code is not taken into consideration

Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138
Amm
  • 1,749
  • 4
  • 17
  • 27

1 Answers1

111

Dont use write.csv, use write.table

 write.table( <yourdf>, sep=",",  col.names=FALSE)

You cannot change many settings in write.csv -- the arguments are just there as a reminder to the user. From the documentation:

These wrappers are deliberately inflexible: they are designed to ensure that the correct conventions are used to write a valid file. Attempts to change append, col.names, sep, dec or qmethod are ignored

Ricardo Saporta
  • 54,400
  • 17
  • 144
  • 178
  • I need to store my data as a csv file though. I tried using col.names = False, but as you say it does not remove the column name. Is there any other way around this? – Amm Oct 07 '13 at 14:25
  • 4
    Yes it works now, thanks. I followed your suggestions and my final code looks like this, write.table( my_df, filename, sep=",", col.names=FALSE). – Amm Oct 07 '13 at 14:40
  • 4
    don't forget your row.names argument – Ricardo Saporta Oct 07 '13 at 14:46
  • 1
    Sure, my code looks like this write.table( my_df, filename, sep=",", col.names=F, row.names=F). The above code creates a .csv file correctly, however when I open the file in Excel, both the columns appear to be in the same column. Is there anyway to have the individual columns appear separately when I open in Excel by default without having to do use the convert "Text to columns" option. – Amm Oct 08 '13 at 08:41
  • Are you adding ".csv" to the end of your your_file? It should open fine in Excel if so. – Seanosapien Mar 05 '19 at 10:33