title: Apply an R script over multiple .txt files in a folder
body: <p>I am extremely new to building functions and loops. I have looked at previous questions that are similar to my issue but I can't seem to find the solution for my problem. My goal is to extract climate data from a webpage like this:</p>
  
  <p><a href="https://mesonet.agron.iastate.edu/cgi-bin/request/coop.py?network=NECLIMATE&amp;stations=NE3065&amp;year1=2020&amp;month1=1&amp;day1=1&amp;year2=2020&amp;month2=12&amp;day2=31&amp;vars%5B%5D=gdd_50_86&amp;model=apsim&amp;what=view&amp;delim=comma&amp;gis=no&amp;scenario_year=2019" rel="nofollow noreferrer">https://mesonet.agron.iastate.edu/cgi-bin/request/coop.py?network=NECLIMATE&amp;stations=NE3065&amp;year1=2020&amp;month1=1&amp;day1=1&amp;year2=2020&amp;month2=12&amp;day2=31&amp;vars%5B%5D=gdd_50_86&amp;model=apsim&amp;what=view&amp;delim=comma&amp;gis=no&amp;scenario_year=2019</a></p>
  
  <p>where I will use this data to calculate growing degree days for a crop growth model. I have had success pulling data using a for loop. </p>
  
  <pre><code>uticaNE &lt;- "https://mesonet.agron.iastate.edu/cgi-bin/request/coop.py?network=NECLIMATE&amp;stations=NE8745&amp;year1=2020&amp;month1=1&amp;day1=1&amp;year2=2020&amp;month2=12&amp;day2=31&amp;vars%5B%5D=gdd_50_86&amp;model=apsim&amp;what=view&amp;delim=comma&amp;gis=no&amp;scenario_year=2019"
  
  friendNE &lt;- "https://mesonet.agron.iastate.edu/cgi-bin/request/coop.py?network=NECLIMATE&amp;stations=NE3065&amp;year1=2020&amp;month1=1&amp;day1=1&amp;year2=2020&amp;month2=12&amp;day2=31&amp;vars%5B%5D=gdd_50_86&amp;model=apsim&amp;what=view&amp;delim=comma&amp;gis=no&amp;scenario_year=2019"
  
  location.urls &lt;- c(uticaNE, friendNE)
  location.meso.files &lt;- c("uticaNe.txt", "friendNE.txt")
  
  for(i in seq_along(location.urls)){
    download.file(location.urls[i], location.meso.files[i], method="libcurl")
  } 
  
  </code></pre>
  
  <p>I will have around 20 locations I will be pulling data in daily. What I want to do is apply a task where I calculate fahrenheit, GDD, etc. to each file and save the output of each file separately.</p>
  
  <p><strong>This is the following code I have currently.</strong></p>
  
  <pre><code>files &lt;- list.files(pattern="*.txt", full.names=TRUE, recursive=FALSE)
  
    func &lt;- for (i in 1:length(files)){
    df &lt;- read.table(files[i], skip=10, stringsAsFactors = 
    FALSE)
    colnames(df) &lt;- c("year", "day", "solrad", "maxC", 
    "minC", "precipmm")
    df$year &lt;- as.f(df$year)
    df$day &lt;- as.factor(df$day)
    df$maxF &lt;- (df$maxC * (9/5) + 32)
    df$minF &lt;- (df$minC * (9/5) + 32)
    df$GDD &lt;- (((df$maxF + df$minF)/2)-50)
    df$GDD[df$GDD &lt;= 0] &lt;- 0
    df$GDD.cumulateive &lt;- cumsum(df$GDD)
    df$precipmm.cumulative &lt;- cumsum(df$precipmm)
    return(df)
    write.table(df, path="./output", quote=FALSE, 
    row.names=FALSE, col.names=TRUE)
  }
  
  data &lt;- apply(files, func)
  </code></pre>
  
  <p>Any help would be greatly appreciated. </p>
  
  <p>-ML</p>