I have an R script that runs a set of functions on multiple datasets and creates a set of concentration-response models. What I would like to do is save out the model parameters and other results to a csv file.
My plan is to have the script create an empty csv file and then as the script progresses, results are appended to that csv file. Every time the script is run I would like the results to be overwritten.
I've tried to create an empty file using
system("copy /y NUL results.csv > NUL")
to create the file, but the file is not created. This command (i.e., copy/y NUL results.csv > NUL
) works properly when run directly in the Windows terminal.
Am I missing something simple? System is Windows XP.
Thanks all!
To create a CSV file with a text editor, first choose your favorite text editor, such as Notepad or vim, and open a new file. Then enter the text data you want the file to contain, separating each value with a comma and each row with a new line.
Is there anything wrong with file.create()
, which is portable across operating systems?
file.create("my.csv")
# [1] TRUE
You can then append to the file, e.g. using the append=TRUE
argument to write.table()
, perhaps like this:
df <- data.frame(a=1:4, b=4:1)
write.table(df, file="my.csv", sep=",", row.names=FALSE, append=TRUE)
write.table(df, file="my.csv", sep=",", row.names=FALSE,
col.names=FALSE, append=TRUE)
EDIT If you will be doing a ton of writes to each file, it can save considerable time to open a connection to the file once, and only close it when you're done. If that is not the case, the approach above works just fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With