title: Using a loop to read, clean, and write, multiple .csv's in r
body: <p>I have multiple (~ 80) .csv's in a common folder that I need to subset certain columns and write the subset-ed new table into a new .csv. I have a script that I can use to do them one at a time: </p>
  
  <pre><code>setwd("C:/Data/HAAS")
  
  ### ENTER COUNTY NAME
  
  county.name &lt;- "charlotte"
  
  
  ####ENTER UNZIPPED CSV DOWNLOADED FROM ftp://sdrftp03.dor.state.fl.us/Tax%20Roll%20Data%20Files/2016%20Final%20NAL%20-%20SDF%20Files/
  
  
  cnty.raw&lt;- read.csv("C:/Data/HAAS/tax_roll_data/NAL18F201601.csv")
  cnty.clean &lt;- ""                                    ## CREATE NEW EMPTY DATAFRAME
  
  
  cnty.clean$CNTY_ID &lt;- cnty.raw$CO_NO                ## ADD COUNTY ID TO CLEAN FILE
  cnty.clean$PARCEL_ID &lt;- cnty.raw$PARCEL_ID          ## ADD PARCEL ID TO CLEAN FILE 
  cnty.clean$ACT_BUILT &lt;- cnty.raw$ACT_YR_BLT         ## ADD ACTUAL YEAR BUILT TO CLEAN FILE
  cnty.clean$FLUCCS &lt;- cnty.raw$DOR_UC                ## ADD LAND USE COLUMN
  cnty.clean$HMSTD_VAL &lt;- cnty.raw$AV_HMSTD           ## USED TO DETERMINE IF PARCEL CONTAINS A HOMESTEAD
  
  cnty.clean &lt;- cnty.clean[cnty.clean$FLUCCS == 001, ]   ## SELECT ONLY SINGLE-FAMILY RESIDENTIAL PROPERTIES 
  cnty.clean &lt;- na.omit(cnty.clean)                      ## REMOVE NA VALUES
  head(cnty.clean)
  
  
  #### BEGIN WRITING CLEANED TABLES TO OUTPUT FOLDERS ##################################
  
  setwd("C:/Data/HAAS/R_output/csv")                    ## CSV DESTINATION FOLDER
  
  
  write.csv(cnty.clean, file = paste(county.name, ".csv", sep = ""))    ## WRITE CSV TO FOLDER
  
  setwd("C:/Data/HAAS/R_output/dbf")                                    ## DBF DESTINATION FOLDER
  library(foreign)
  
  write.dbf(cnty.clean, file = paste(county.name, ".dbf", sep = ""))    ## WRITE AS .DBF FOR SHAPEFILE MERGE
  
  #### RESET WORKING DIRECTORY BACK TO ORIGINAL PATH
  
  setwd("C:/Data/HAAS")
  
  head(cnty.clean)
  summary(cnty.clean)
  </code></pre>
  
  <p>the problem is that I have to manualy change the csv file name and also the county name field manually everytime I want to run the code through all of the csv's. </p>
  
  <p>I have used r to do simple tasks, but am an amateur when it comes to automating processes using loops. Is there a way I can insert this code into a loop to run over and over again through all the csv's in the filepath's folder? </p>
  
  <p>NOTE: I have searched around and tried different options in other posts, but none seem to work for my situation.</p>