I'm new to stackoverflow and relatively new to R. Please point out anything I am doing wrong, or could do better, to ask my question on this forum - thanks!
I have a matrix in R with data that has been summarized by year and month, and I want to write out one file per year, with 12 rows per file (one row for each month), along with some additional text into each output file.
I have looked at a couple different solutions on this forum:
this one explains a technique with tapply to create a list of matrices of salaries by name, then write them to a file: Write many files in a for loop
this one shows how to add multiple lines of text to a file: Write lines of text to a file in R
These have elements of what I want to do, but tapply seems to be limited to one column ("salary" in the example), and I want to grab 3 to write out. I'm also mixing writing data and free-form text.
My data in R looks like this:
Month | Year | Precip | Temp | PanEvap
01 | 2018 | 20 | -4 | 12
02 | 2018 | 15 | 0 | 10
03 | 2018 | 60 | 5 | 40
... etc
(sorry for the lack of formatting - not sure how to make a table, or use fixed-width font in the final posting!)
I need to output one file for each year like this (this would be called "18.dat", the "18" to be taken from the year, "2018"):
"Some text up here"
20 -4 12
15 0 10
60 5 40
...etc
"More text down here"
I hope this makes sense. Any guidance and sample code would be very much appreciated.
Thanks!