[R] Adding a header after the file is written

Ista Zahn istazahn at gmail.com
Tue May 4 00:28:32 CEST 2010

Hi Steve,
I think you just need to set col.names = FALSE (instead of col.names
=NULL) on subsequent writes.


On Mon, May 3, 2010 at 5:19 PM, steven mosher <moshersteven at gmail.com> wrote:
> The situation arises where I open a file to write a data.frame to it. with
> write.table.
> multiple lines are written to the file and the file is kept in Append=TRUE
> mode.
> If one sets the col.names to the names of the variables being written,  you
> have output
> that looks like this...
> name1 name2  name3.....
>  x          x         x
>  x          x         x
>  x          x         x
> name1  name2  name 3
>  x          x         x
>  x          x         x
>  x          x         x
> And so forth as each time write is called, the col.names are written.
> Setting col.names=NULL obviously removes them.
> I thought a simple solution would be to check for the file existence first
> and on the first write, include the col.names. with append=T.
> on subsequent writes, col.names would be set to NULL.
> that didnt work and threw warnings.
> Is there anyway to do this. basically open a file for writing, with
> append=TRUE and only write the col.names once
> at the first write. or am I stuck and forced to write the whole file without
> the col.names and then read back in and rewrite
> with col.names="the cols names I want"
>        [[alternative HTML version deleted]]
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology

More information about the R-help mailing list