Re: [R] Adding a header after the file is written

2010-05-03 Thread steven mosher
thanks.. worked

On Mon, May 3, 2010 at 3:24 PM, Ted Harding wrote:

> On 03-May-10 21:19:34, steven mosher 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"
>
> The following (which uses a tiny dataframe I had lying around after
> responding to an earlier query) looks like what you want to do
> (provided you first test existince of the file before switching
> to the second form of write.table()):
>
>  foo
>  # $Bar1
>  # [1] 1
>  # $Bar2
>  # [1] 2
>  # $Bar3
>  # [1] 3
>  # $Bar4
>  # [1] 4
>
> write.table(foo,file="foo.txt",row.names=FALSE,
>col.names=c("Bar.1","Bar.2","Bar.3","Bar.4"),
>append=FALSE)
> write.table(foo,file="foo.txt",row.names=FALSE,
>col.names=FALSE,append=TRUE)
> write.table(foo,file="foo.txt",row.names=FALSE,
>col.names=FALSE,append=TRUE)
> write.table(foo,file="foo.txt",row.names=FALSE,
>col.names=FALSE,append=TRUE)
> write.table(foo,file="foo.txt",row.names=FALSE,
>col.names=FALSE,append=TRUE)
>
>
> Contents of foo.txt after the above:
>
>  "Bar.1" "Bar.2" "Bar.3" "Bar.4"
>  1 2 3 4
>  1 2 3 4
>  1 2 3 4
>  1 2 3 4
>  1 2 3 4
>
> Ted.
>
> 
> E-Mail: (Ted Harding) 
> Fax-to-email: +44 (0)870 094 0861
> Date: 03-May-10   Time: 23:24:55
> -- XFMail --
>

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] Adding a header after the file is written

2010-05-03 Thread steven mosher
Ok I will try that. I think I set it to False when I tried it the first
time, maybe that was my mistake

On Mon, May 3, 2010 at 3:28 PM, Ista Zahn  wrote:

> Hi Steve,
> I think you just need to set col.names = FALSE (instead of col.names
> =NULL) on subsequent writes.
>
> -Ista
>
> On Mon, May 3, 2010 at 5:19 PM, steven mosher 
> 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@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
> http://yourpsyche.org
>

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] Adding a header after the file is written

2010-05-03 Thread Ista Zahn
Hi Steve,
I think you just need to set col.names = FALSE (instead of col.names
=NULL) on subsequent writes.

-Ista

On Mon, May 3, 2010 at 5:19 PM, steven mosher  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@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
http://yourpsyche.org

__
R-help@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.


Re: [R] Adding a header after the file is written

2010-05-03 Thread Ted Harding
On 03-May-10 21:19:34, steven mosher 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"

The following (which uses a tiny dataframe I had lying around after
responding to an earlier query) looks like what you want to do
(provided you first test existince of the file before switching
to the second form of write.table()):

  foo
  # $Bar1
  # [1] 1
  # $Bar2
  # [1] 2
  # $Bar3
  # [1] 3
  # $Bar4
  # [1] 4

write.table(foo,file="foo.txt",row.names=FALSE,
col.names=c("Bar.1","Bar.2","Bar.3","Bar.4"),
append=FALSE)
write.table(foo,file="foo.txt",row.names=FALSE,
col.names=FALSE,append=TRUE)
write.table(foo,file="foo.txt",row.names=FALSE,
col.names=FALSE,append=TRUE)
write.table(foo,file="foo.txt",row.names=FALSE,
col.names=FALSE,append=TRUE)
write.table(foo,file="foo.txt",row.names=FALSE,
col.names=FALSE,append=TRUE)


Contents of foo.txt after the above:

  "Bar.1" "Bar.2" "Bar.3" "Bar.4"
  1 2 3 4
  1 2 3 4
  1 2 3 4
  1 2 3 4
  1 2 3 4

Ted.


E-Mail: (Ted Harding) 
Fax-to-email: +44 (0)870 094 0861
Date: 03-May-10   Time: 23:24:55
-- XFMail --

__
R-help@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.