Quantcast

Adding header lines to a dataframe that is exported using write.csv

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Adding header lines to a dataframe that is exported using write.csv

mtb954
I would like to export a dataframe to a .csv using:

>write.csv(dataframe,"dataframe.csv")

but I need to add four "header" lines to the csv that are not part of
the dataframe (which itself has a line of column headers).

The difficulty (for me, at least!) lies in the requirement that
certain elements of the header (X, Y and the number of "Q"s - please
see example below) must be defined based on the number of rows and
columns in the dataframe, which vary depending on the input file.

Here's what the 3 .csv header lines should look like, followed by a
number of dataframe rows (i.e., these lines are not R code, but are
what R will produce).

X, plots ,,,, #where X=number of rows in the dataframe
Y, species,,,, #where Y=number of columns in the dataframe
,Q,Q,Q,Q,Q #where the number of Qs=the number of columns in the dataframe

Those 3 .csv header lines would be followed by dataframe, which
consists of one row containing column headers and X "data" rows:

,spec1,spec2,spec3,sp3c4,spec5 #these are the dataframe's column headers
plot1,15.84,0,0,792,7 #this is an example "data" row

In case the above is unclear, I have also attached a small .csv as an
example of what the output should look like.

Thank you. Mark

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Adding header lines to a dataframe that is exported using write.csv

Duncan Murdoch
On 2/25/2006 1:20 PM, Mark wrote:
> I would like to export a dataframe to a .csv using:
>
>> write.csv(dataframe,"dataframe.csv")
>
> but I need to add four "header" lines to the csv that are not part of
> the dataframe (which itself has a line of column headers).

Open a connection and write the header to it first, then write the
dataframe.  For example,

df <- data.frame(a=1:5,b=6:10)

f <- file("dataframe.csv", "w")

writeLines(paste(c(nrow(df), ncol(df)), c("plots", "species"), ",,,",
sep=","),f)
writeLines(paste(rep(",Q", ncol(df)), collapse=""),f)
write.csv(df, f)

close(f)

Duncan Murdoch

>
> The difficulty (for me, at least!) lies in the requirement that
> certain elements of the header (X, Y and the number of "Q"s - please
> see example below) must be defined based on the number of rows and
> columns in the dataframe, which vary depending on the input file.
>
> Here's what the 3 .csv header lines should look like, followed by a
> number of dataframe rows (i.e., these lines are not R code, but are
> what R will produce).
>
> X, plots ,,,, #where X=number of rows in the dataframe
> Y, species,,,, #where Y=number of columns in the dataframe
> ,Q,Q,Q,Q,Q #where the number of Qs=the number of columns in the dataframe
>
> Those 3 .csv header lines would be followed by dataframe, which
> consists of one row containing column headers and X "data" rows:
>
> ,spec1,spec2,spec3,sp3c4,spec5 #these are the dataframe's column headers
> plot1,15.84,0,0,792,7 #this is an example "data" row
>
> In case the above is unclear, I have also attached a small .csv as an
> example of what the output should look like.
>
> Thank you. Mark
>
>
> ------------------------------------------------------------------------
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Adding header lines to a dataframe that is exported using write.csv

Henrik Bengtsson-2
In reply to this post by mtb954
Just a tips: When you add headers to tabulate files like yours, it is
convenient to start each header line with a '#' (like an R comment),
because then read.table() will not complain about the header lines.
It is easy to strip the '#' off the header lines, i.e. grep("^#", "",
hlines) before further parsing.

/Henrik

On 2/25/06, Mark <[hidden email]> wrote:

> I would like to export a dataframe to a .csv using:
>
> >write.csv(dataframe,"dataframe.csv")
>
> but I need to add four "header" lines to the csv that are not part of
> the dataframe (which itself has a line of column headers).
>
> The difficulty (for me, at least!) lies in the requirement that
> certain elements of the header (X, Y and the number of "Q"s - please
> see example below) must be defined based on the number of rows and
> columns in the dataframe, which vary depending on the input file.
>
> Here's what the 3 .csv header lines should look like, followed by a
> number of dataframe rows (i.e., these lines are not R code, but are
> what R will produce).
>
> X, plots ,,,, #where X=number of rows in the dataframe
> Y, species,,,, #where Y=number of columns in the dataframe
> ,Q,Q,Q,Q,Q #where the number of Qs=the number of columns in the dataframe
>
> Those 3 .csv header lines would be followed by dataframe, which
> consists of one row containing column headers and X "data" rows:
>
> ,spec1,spec2,spec3,sp3c4,spec5 #these are the dataframe's column headers
> plot1,15.84,0,0,792,7 #this is an example "data" row
>
> In case the above is unclear, I have also attached a small .csv as an
> example of what the output should look like.
>
> Thank you. Mark
>
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
>


--
Henrik Bengtsson
Mobile: +46 708 909208 (+1h UTC)

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Adding header lines to a dataframe that is exported using write.csv

Sean Davis



On 2/27/06 6:03 AM, "Henrik Bengtsson" <[hidden email]> wrote:

> Just a tips: When you add headers to tabulate files like yours, it is
> convenient to start each header line with a '#' (like an R comment),
> because then read.table() will not complain about the header lines.
> It is easy to strip the '#' off the header lines, i.e. grep("^#", "",
> hlines) before further parsing.

You can also use the skip argument to read.table() to skip an arbitrary
number of lines of header information.

Sean

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Loading...