This post shows how to use RClimate.txt to retrieve a climate time series and write a csv file in 5 lines of R script.
One of my readers, Robert, wants to be able to download climate time series data and write it to a csv file. The R script below shows how to download the MEI data series and write a csv file. For this example I will use the RClimate function (func_MEI) to retrieve the data. I then simply specify the path and file name link for the output file (note quotes around the output file name and then write a csv file.
source("http://processtrends.com/files/RClimate.txt" m <- func_MEI() head(m) output_link <- "C://R_Home/mei.csv" write.csv(m, output_link, quote=FALSE, row.names = F)
Let’s walk through the lines to see what is going on:
This line is telling R to read and have available the R script functions in the source file.
m <- func_MEI()
This line is telling R to assign the results of func_MEI() to the m data.frame. I have added 20 climate time series to RClimate similar to func_MEI.
This line displays the first 6 lines of the m data.frame so that we can be sure that func_MEI() returned some data.
output_link <- “C://R_Home/mei.csv”
This line assigns the target file path and name where I want the csv file saved.
write.csv(m, output_link, quote=FALSE, row.names = F)
This line writes the m data to a csv file located at the path and fie name specified in output_link.
If I want to see the func_MEI() R script, I just have to enter func_MEI in the R console and R will display the func_MEI() script on the console.