Biocrazy
Biocrazy

Reputation: 401

How to specify .csv delimiter while using Map()

I have a list containing 2 or more dataframes:

d <- data.frame(x=1:3, y=letters[1:3])
f <- data.frame(x=11:13, y=letters[11:13])
df <- list(d, f)

to save them as .csv, I use the following syntax:

filenames = paste0('C:/Output_', names(df), '.csv')
Map(write.csv, df, filenames)

But I would like to add some strings to obtain a specific format, like:

quote = FALSE, row.names = FALSE, sep = "\t", na = "", col.names = FALSE

And the thing is that I am not that sure where to add that syntax. Wherever I try, I get a warning saying my syntax has been ignored.

> Warning messages:
1: In (function (...)  : attempt to set 'col.names' ignored
2: In (function (...)  : attempt to set 'sep' ignored
3: In (function (...)  : attempt to set 'col.names' ignored
4: In (function (...)  : attempt to set 'sep' ignored

Any suggestions? In BaseR preferably!

Upvotes: 1

Views: 192

Answers (2)

Sotos
Sotos

Reputation: 51592

You need to use anonymous function in order to be able to pass further arguments, i.e.

Map(function(...) write.csv(..., quote = FALSE, row.names = FALSE, sep = "\t", na = ""), df, filenames)

Upvotes: 3

Ben Bolker
Ben Bolker

Reputation: 226332

Why you're still getting col.names warnings: farther down in the documentation (?write.csv) you'll see

These wrappers [write.csv and write.csv2] 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.

Should go away if you use write.table() instead.

Upvotes: 5

Related Questions