Home > other >  Unable to concatenate header string to write.table output in R
Unable to concatenate header string to write.table output in R

Time:06-06

I am trying to concatenate a custom string to all the CSV files that I am generating like below.

myHeadTXT <- "Fixed format string
concatenation test."

# basepath = C:/temp (for example)
# 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) {
  cat(header = myHeadTXT, '\n',  file = file)
  write.csv(subset(i, select = -c(Grp, Subgrp)),
            file.path(basepath, i$Grp[1], i$Subgrp[1], "value.csv"),
            row.names = FALSE, quote = FALSE, append = TRUE)
})

and I get the following error:

Error in cat(header = myHeadTXT, "\n", file = file) : 
argument "file" is missing, with no default

Here is the sample data for code and error reproducibility.

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)

My desired (sample) output in each value.csv:

Fixed format string
concatenation test.
val1
1.1

CodePudding user response:

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)
  •  Tags:  
  • r
  • Related