Kumar
Kumar

Reputation: 304

How to write multiple xtabs in a CSV file?

For writing a single xtab, I have used:

write.csv(xtabData1, "analysis.csv")

For appending one more xtab to the same csv file, I tried:

write.csv(xtabData2, "analysis.csv", append=T)

But this throws a warning "attempt to set 'append' ignored" and overwrites the csv file.

Upvotes: 1

Views: 898

Answers (2)

Richard Herron
Richard Herron

Reputation: 10112

The append option is disabled in write.csv(). write.csv() is just a wrapper function for write.table(). Here;s more from the help file.

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. write.csv uses "." for the decimal point and a comma for the separator. write.csv2 uses a comma for the decimal point and a semicolon for the separator, the Excel convention for CSV files in some Western European locales. 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.

Use write.table() instead (with sep="," and whatever other settings you'd like).

Upvotes: 1

Michael Dunn
Michael Dunn

Reputation: 8313

One solution would be to join the tab data first using rbind, e.g.

write.csv(rbind(xtabData1, xtabData2), file="analysis.csv")

Upvotes: 1

Related Questions