Earl F. Glynn
2007-Oct-31 23:04 UTC
[R] Don't understand write.csv default: Why is column name for column of row names blank?
I've always been frustrated that R never puts a label on the column name for
a column of row names. This is usually the "key" in a database table,
and
it sure would be nice to carry a "key" name along with the data
instead of
the field being blank. So, why is it a good idea for it to be blank?
?write.csv says this:
CSV files
By default there is no column name for a column of row names. If
col.names = NA and row.names = TRUE
a blank column name is added, which is the convention used for CSV files
to be read by spreadsheets.
This last sentence doesn't make any sense to me: "the convention used
for
CSV files to be read by spreadsheets". Spreadsheets don't care whether
this
column is blank or has useful information. R may have a convention to leave
the column name for a column of row names blank (I don't understand why),
but most other applications do not.
I seem to spend a lot of time trying to get this blank field "fixed"
since
it's just not right to have a database "key" be blank. I often
must
manually edit the .csv file written by R since that's easier than the kludge
to put the column names in a new column one so they can have a column name
too, and then suppress the row names in the write.csv.
What am I missing? Is there an easy way to fill this blank field with a
useful name for a database key when using write.csv defaults?
Thanks for any insight on this.
efg
Earl F. Glynn
Scientific Programmer
Stowers Institute for Medical Research
Gabor Grothendieck
2007-Oct-31 23:35 UTC
[R] Don't understand write.csv default: Why is column name for column of row names blank?
Would this be good enough: # example using builtin BOD data frame write.csv(cbind(Z = row.names(BOD), BOD), row.names = FALSE) On Oct 31, 2007 7:04 PM, Earl F. Glynn <efg at stowers-institute.org> wrote:> I've always been frustrated that R never puts a label on the column name for > a column of row names. This is usually the "key" in a database table, and > it sure would be nice to carry a "key" name along with the data instead of > the field being blank. So, why is it a good idea for it to be blank? > > ?write.csv says this: > > CSV files > By default there is no column name for a column of row names. If > col.names = NA and row.names = TRUE > a blank column name is added, which is the convention used for CSV files > to be read by spreadsheets. > > > This last sentence doesn't make any sense to me: "the convention used for > CSV files to be read by spreadsheets". Spreadsheets don't care whether this > column is blank or has useful information. R may have a convention to leave > the column name for a column of row names blank (I don't understand why), > but most other applications do not. > > I seem to spend a lot of time trying to get this blank field "fixed" since > it's just not right to have a database "key" be blank. I often must > manually edit the .csv file written by R since that's easier than the kludge > to put the column names in a new column one so they can have a column name > too, and then suppress the row names in the write.csv. > > What am I missing? Is there an easy way to fill this blank field with a > useful name for a database key when using write.csv defaults? > > Thanks for any insight on this. > > efg > > Earl F. Glynn > Scientific Programmer > Stowers Institute for Medical Research > > ______________________________________________ > 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. >
jim holtman
2007-Oct-31 23:37 UTC
[R] Don't understand write.csv default: Why is column name for column of row names blank?
The nice thing about R is that you can always extend it in any manner
that you want. Here is some code (that you could put in a function
wrapper) that will do what you want. It just creates the header,
writes it out, and then the dataframe itself without a column header:
out <- file('/tempxx.csv', 'w') #output file
header <- paste(c("DB KEY", names(BOD)), collapse=',') #
column names
cat(header, '\n', file=out) #write it out
write.table(BOD, out, sep=',', col.names=FALSE) #rest of table
close(out)
On 10/31/07, Earl F. Glynn <efg at stowers-institute.org>
wrote:> I've always been frustrated that R never puts a label on the column
name for
> a column of row names. This is usually the "key" in a database
table, and
> it sure would be nice to carry a "key" name along with the data
instead of
> the field being blank. So, why is it a good idea for it to be blank?
>
> ?write.csv says this:
>
> CSV files
> By default there is no column name for a column of row names. If
> col.names = NA and row.names = TRUE
> a blank column name is added, which is the convention used for CSV files
> to be read by spreadsheets.
>
>
> This last sentence doesn't make any sense to me: "the convention
used for
> CSV files to be read by spreadsheets". Spreadsheets don't care
whether this
> column is blank or has useful information. R may have a convention to
leave
> the column name for a column of row names blank (I don't understand
why),
> but most other applications do not.
>
> I seem to spend a lot of time trying to get this blank field
"fixed" since
> it's just not right to have a database "key" be blank. I
often must
> manually edit the .csv file written by R since that's easier than the
kludge
> to put the column names in a new column one so they can have a column name
> too, and then suppress the row names in the write.csv.
>
> What am I missing? Is there an easy way to fill this blank field with a
> useful name for a database key when using write.csv defaults?
>
> Thanks for any insight on this.
>
> efg
>
> Earl F. Glynn
> Scientific Programmer
> Stowers Institute for Medical Research
>
> ______________________________________________
> 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.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem you are trying to solve?
Maybe Matching Threads
- RODBC and Excel: Wrong Data Type Assumed on Import
- Why only a "" string for heading for row.names with write.csv with a matrix?
- Read Windows-like .INI files into R data structure?
- french secondary boxplot
- Partek has Dunn-Sidak Multiple Test Correction. Is this the same/similar to any of R's p.adjust.methods?