title: Aggregate (Summarize) multiple Time Series Data by Month in .r
body: <p>I have hundreds of daily weather data with a .txt extension, with comma (",") as separators in common folders. Each file has the same data structure with different file names. The following is an example of the data structure:</p>
  
  <pre><code>$ year         : int  1980 1980 1980 1980 1980 1980 1980 1980 1980 1980 ...
  $ month        : int  1 1 1 1 1 1 1 1 1 1 ...
  $ day          : int  1 2 3 4 5 6 7 8 9 10 ...
  $ V1           : num  18.4 22.9 19.9 22.9 23.4 9.8 13.9 17.5 20.3 22.7 ...
  $ V2           : num  30.8 31.5 31.4 31.3 31.5 29.8 30.1 30.6 30.5 31.1 ...
  $ V3           : num  23.4 23.7 23.2 23.3 23.4 22.9 23 23.4 23.1 23.2 ...
  $ V4           : num  2.2 0 0 0 0.9 3.6 3.5 3.7 1.2 0 ...
  $ V5           : num  0.93 0.86 0.88 0.87 0.87 0.98 1 0.96 0.96 0.91 ...
  $ V6           : num  1.6 3.5 5.2 5.5 3.9 4.2 4.2 4.9 4.9 4.4 ...
  </code></pre>
  
  <p>I need to make a summary of the total monthly of one of the variables, (let say V4) from each file. And the desired output data structure of each file is like this (first column is year, the second column is month, the third column is total of the daily value of V4):</p>
  
  <pre><code>Year 1  Month 1 22.1
  Year 1  Month 2 82.4
  Year 1  Month 3 142.8
  Year 1  Month …etc  314
  Year 2  Month 1 48.9
  Year 2  Month 2 173.6
  Year 2  Month 3 76.2
  Year 2  Month …etc  517.4
  Year 3  Month 1 117.8
  Year 3  Month 2 20.1
  Year 3  Month 3 169.8
  Year 3  Month …etc  191.5
  </code></pre>
  
  <p>Then i need export the results to be a unique .txt file from all files, with the name of the new file according to the original file (example: before_file1.txt into result_file1.txt) for each file.
  I have a script using Purrr, but nothing seems to be happening. Please if you were willing to help me improve the script with the right method. Thank you</p>
  
  <pre><code># Load packages
  library(tidyverse)
  library(dplyr)
  library(purrr)
  
  # Setting working directory
  workingdirectory &lt;- "D:/Directory"
  setwd(workingdirectory)
  
  # Listing the files in the folder with .txt extension
  FilesList &lt;- list.files(workingdirectory, pattern = "\\.txt$", full.names = TRUE)
  
  # Looping per files
  purrr::map(FilesList, ~{
   .x %&gt;%
      # Read csv file
      read.csv(sep = ",", header = FALSE, stringsAsFactors = FALSE) %&gt;% 
      # select variables 
      variables &lt;- c("year", "month", "day", "V4") %&gt;% 
      # summarize monthly of V4
      group_by(month, year) %&gt;% 
      summarise(monthly = sum(V4)) %&gt;% 
  
  })
  
  # Write the data back
  write.csv(paste0('Result_', basename(.x)), sep = ",", row.names = FALSE)
  </code></pre>
  
  <p>I have edited the script, but there is an error. Please help to fix it. Thanks</p>
  
  <pre><code>Error: unexpected '}' in:
  "    
  }"
  &gt; 
  &gt; # Write the data back
  &gt; write.csv(paste0('TM_', basename(.x)), sep = ",", row.names = FALSE)
  Error in basename(.x) : object '.x' not found
  In addition: Warning message:
  In write.csv(paste0("TM_", basename(.x)), sep = ",", row.names = FALSE) :
    attempt to set 'sep' ignored
  </code></pre>