[R] Don't understand write.csv default: Why is column name for column of row names blank?

jim holtman jholtman at gmail.com
Thu Nov 1 00:37:54 CET 2007


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?



More information about the R-help mailing list