Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write a data frame to csv file without column header in R [duplicate]

Tags:

r

col

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

like image 517
Amm Avatar asked Oct 07 '13 14:10

Amm


People also ask

How do I save a DataFrame without column names?

pandas to CSV without Header To write DataFrame to CSV without column header (remove column names) use header=False param on to_csv() method.

How do I remove the index and header from a data frame?

Just simply put header=False and for eliminating the index using index=False.

How do I remove column names in R?

In R, the easiest way to remove columns from a data frame based on their name is by using the %in% operator. This operator lets you specify the redundant column names and, in combination with the names() function, removes them from the data frame. Alternatively, you can use the subset() function or the dplyr package.


1 Answers

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

like image 131
Ricardo Saporta Avatar answered Sep 22 '22 13:09

Ricardo Saporta