简体   繁体   中英

Add additional information between appended write.table outputs in R

I am trying to output a series of tables to a CSV file using the append=TRUE argument of the write.table function. However I would like to add additional information between the tables (eg. table captions).

I'm very new to R and don't know how to use loops/define functions, etc. so do everything line by line, so would appreciate solutions to be in a similar simple format so that I am able to manipulate the code for other uses later.

My current code is:

table1 <- aggregate(myData[,7:11],by=list(myData$Variable1), min)
table2 <- aggregate(myData[,7:11],by=list(myData$Variable1), median)
table3 <- aggregate(myData[,7:11],by=list(myData$Variable1), max)
write.table(table1, file="myDataTables.csv", append = FALSE, sep=",", row.names=FALSE)
write.table(table2, file="myDataTables.csv", append = TRUE, sep=",", row.names=FALSE)
write.table(table3, file="myDataTables.csv", append = TRUE, sep=",", row.names=FALSE)

My csv file then comes out like this:

table 1 column names
table 1 data (10 rows)
table 2 column names
table 2 data (10 rows)
table 3 column names
table 3 data (10 rows)

What I'd like it to look like (including the blank rows) is:

Table 1 caption (eg. "Table 1: Table name")
table 1 column names
table 1 data (10 rows)

Table 2 caption (eg. "Table 2: Table name")
table 2 column names
table 2 data (10 rows)

Table 3 caption (eg. "Table 3: Table name")
table 3 column names
table 3 data (10 rows)

When I was sending output to .txt files, I used the cat function to add in captions and blank lines as in the following:

out<-capture.output(table1)
cat("Table 1: Table Name","",out,"","",file="myDataTables.txt",sep="\n",append=TRUE)

I'd like to be able to do something like this when writing CSV files.

The simple solution, which is more or less the code you included in your question.

co2_mins <- aggregate(. ~ Plant, CO2, min)
co2_medians <- aggregate(. ~ Plant, CO2, median)
co2_maxs <- aggregate(. ~ Plant, CO2, max)

out_file <- "myDataTables.csv"
sep <- ","
cat("Table 1: CO2 minima by plant\n", file = out_file)
write.table(co2_mins, out_file, append = TRUE, sep = sep)
cat("\nTable 2: CO2 medians by plant\n", file = out_file, append = TRUE)
write.table(co2_mins, out_file, append = TRUE, sep = sep)
cat("\nTable 3: CO2 maxima by plant\n", file = out_file, append = TRUE)
write.table(co2_mins, out_file, append = TRUE, sep = sep)

Only slightly fancier is to use a for loop. This is more easily generalisable, because you can add or remove new summary statistics with ease. It's also less prone to mistakes, but you aren't copying and pasting code several times. (This follows the principle of don't repeat yourself .)

fns <- list(minima = min, medians = median, maxima = max)
for(i in seq_along(fns))
{
  summary_stats <- aggregate(. ~ Plant, CO2, fns[[i]])
  cat(
    if(i > 1) "\n",
    "Table ", i, ": CO2 ", names(fns)[i], " by plant\n", 
    file = out_file, 
    sep = "",
    append = i > 1
  )
  write.table(
    summary_stats, 
    out_file, 
    append = TRUE, 
    sep = ","
  )
}

I should probably point out that what you want is a bit weird. CSV files are great for storing data, but not for reading by humans. Having this situation of multiple CSV files plus captions for human reading makes it difficult to to use programmatically, so now you have a file that is hard to read and bad for data storage.

It's better to keep your data stored in individual CSV files, and then use the knitr package to autogenerate a report from your data.

I dont know if this solution is working in your particular example, but it seems to work pretty wellinmy case (except from outputing some warning messsages for some events):

Lets say you have wrapped all your tables into a list, and then you have a "for loop" to append them in a single csv file. Then you may use that, to produce an "empty row", into each written csv table:

list_tables 
[[1]]
Table1 
[[2]]
Table2
[[3]]
Table3
[[4]]
Table4

 write.table( rbind(list_tables[[i]], ""), path-filename, append=T, ....) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM