Write.Table Writes Unwanted Leading Empty Column to Header When Has Rownames

write.table writes unwanted leading empty column to header when has rownames

Citing ?write.table, section 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.

So you must do

write.table(a, 'a.txt', col.names=NA)

and you get

"" "A" "B" "C"
"A" 1 4 7
"B" 2 5 8
"C" 3 6 9

write.table is not outputting a header for row names

Per the help for write.table, you want to specify col.names=NA:

write.table(merged.pca$rotation, file="rotation.csv", col.names=NA, sep=",")

Yes, I think it's a bit silly too. Note that write.csv will do this for you automatically.

Data frame writes to a single column in write.csv/write.table R

write.table()'s standard separator is " " (check the docs).
Use write.csv() or write.csv2() along with the parameter row.names=False instead.

write.csv(sample,file = "my_dir/my_file.csv", row.names=F)

row.names=F makes that a unique row identifier (basically an id like you have it) will not be written.

You may use write.table() as well but you'll have to pass additional parameters:

write.table(sample, file = "test.csv", row.names=F, sep=",", dec=".")

Unable to concatenate header string to write.table output in R

The error in the question is about the anonymous function called by by not finding its argument file. It won't find header either, it is not passed to it.

  • The first argument are the groups the data is split into.
  • The extra arguments are passed after the function. In the case below I set a value for header.

As for the file, it is not a function argument, the function assembles it in its body.

There is another error, the use of write.csv with append set. From the documentation, my emphasis.

write.csv and write.csv2 provide convenience wrappers for writing CSV files. They set sep and dec (see below), qmethod = "double", and col.names to NA if row.names = TRUE (the default) and to TRUE otherwise.

[...]

These wrappers are deliberately inflexible: they are designed to ensure that the correct conventions are used to write a valid file. Attempts to change append, col.names, sep, dec or qmethod are ignored, with a warning.

Grp <- c("A", "A", "A", "B", "B", "B")
Subgrp <- c("k", "l", "m", "n", "n", "n")
val1 <- c(1.1, 3.2, 4.5, 5.6, 6.7, 7.7)
df <- data.frame(Grp, Subgrp, val1)

myHeadTXT <- "Fixed format string
concatenation test."

basepath <- "~/Temp"
# this variable tells what directories and
# files are to be deleted after testing the code
newdirs <- unique(file.path(basepath, df$Grp, df$Subgrp))

# Create directories
for (p in unique(file.path(basepath, df$Grp, df$Subgrp))) dir.create(p, recursive = TRUE)

by(df, df$Subgrp, FUN = function(i, header) {
file <- file.path(basepath, i$Grp[1], i$Subgrp[1], "value.csv")
cat(header, '\n', file = file)
write.table(
subset(i, select = -c(Grp, Subgrp)),
file = file,
sep = ",",
row.names = FALSE,
quote = FALSE,
append = TRUE
)
}, header = myHeadTXT)

Avoid quotation marks in column and row names when using write.table

To quote from the help file for write.table

quote

a logical value (TRUE or FALSE) or a numeric vector. If TRUE,
any character or factor columns will be surrounded by double quotes.
If a numeric vector, its elements are taken as the indices of columns
to quote. In both cases, row and column names are quoted if they are
written. If FALSE, nothing is quoted.

Therefore

write.table(df,"datamod.txt",row.names=FALSE,sep="\t", quote = FALSE)

should work nicely.

Function write.table() is converting my character column to numeric when exporting to excel. How can I prevent this?

As mentioned in the comments:

This works.

library(openxlsx)
write.xlsx(df, file = "dummy.xlsx")

Even though the data in your csv has quotes around the Material column, excel likes to think for you and not honour the character quotes. Now you could use libreoffice with calc. This will open the text import option where you can tell it that the first column is text and it will retain the correct format for the Material column.

But the easiest is just to use the above code.



Related Topics



Leave a reply



Submit