title: Why is my loop in r is just running once?
body: <p>I asked a very similar question before but the answers i got dont seem to apply in this case. The aim of my code is primarily to take a file, manipulate it and the save the manipulated file over the old file. Unfortunately there are a lot of file so I have incorporated a for loop but it is stopping after just one run through the loop. I think my return function is in the right place and my for statement worked in a previous slightly different version of the script.
   Here is my code:</p>
  
  <pre><code>AddLatLon&lt;- function(num, condition){
  
  
  #Set working directiory 
  # num is the number of files needing extraction e.g (3:5) , c(2,7)
  
  connect &lt;- file("./EOBS DATA/sources.txt", "r")
  locdata &lt;- readLines(connect)
  close(connect)
  
  info &lt;- locdata[24:length(locdata)] # removes non data before the data begins
  
  
  
  Infodata &lt;- read.table(text = info, sep=',',fill=TRUE,colClasses='character',header ==TRUE )
  InfoTable &lt;- read.csv("./EOBS DATA/sources.csv")  
  InfoTable$STAID &lt;- as.numeric(InfoTable$STAID)
  
  
  
  for(i in c(num)){
  filename &lt;-paste("./EOBS DATA/",condition, "_csv_data/", condition,i, ".csv", sep = "")
  
  
  #if(i &lt;10){
    #filename &lt;- paste("./EOBS DATA/ECA_blend_", condition, "/" ,CONDITION, "_STAID00000", i, ".txt", sep = "")
  #}
  #if(i &gt;=10 &amp; i &lt; 100){
    #filename &lt;- paste("./EOBS DATA/ECA_blend_", condition, "/" ,CONDITION, "_STAID0000", i, ".txt", sep = "")
  #}
  
  #if(i&gt;= 100 &amp; i &lt;1000){
    #filename &lt;- paste("./EOBS DATA/ECA_blend_", condition, "/" ,CONDITION, "_STAID000", i, ".txt", sep = "")
  #}
  #if(i&gt;= 1000){
    #filename &lt;- paste("./EOBS DATA/ECA_blend_", condition, "/" ,CONDITION, "_STAID00", i, ".txt", sep = "")
  #}
  if(file.exists(filename) == FALSE) {
    next
  }
  
  
  #con &lt;- file(filename, "r")
  #data &lt;- readLines(con)
  #close(con)
  #q &lt;- data[21:length(data)] # removes non data before the data begins
  
  
  
  #Impactdata &lt;- read.table(text = q, sep=',',fill=TRUE,colClasses='character',header = TRUE )
  
  
  x &lt;- read.csv(filename)
  
  
  
  point &lt;- match(i, InfoTable$STAID) 
  Lat &lt;- InfoTable[point,5]
  Lon &lt;- InfoTable[point,6]
  
  Lat &lt;- as.character(Lat)
  Lon &lt;- as.character(Lon)
  
  
  x$Lat &lt;- Lat
  x$Lon &lt;- Lon   
  
  
  
  x$X &lt;- NULL
  x$DATE&lt;- as.Date(as.character(x$DATE), format='%Y%m%d')
  
  Savename &lt;- paste("./EOBS DATA/",condition, "_csv_data/", condition,i, ".csv", sep = "")   
  
  if(condition == "rr"){
    condition &lt;- "Precipitation"
  }
  if(condition == "tn"){
    condition &lt;- "Minimum Temperature"
  }
  if(condition == "tx"){
    condition &lt;- "Maximum Temperature"
  }
  
  names(x)&lt;- c("Station_ID", "Source_ID", "Date(yyyy-mm-dd)", condition, "Quality_code(0='valid'; 1='suspect')", "Latitude", "Longitude")
  
  
  write.csv(x, Savename)
  
  
  } 
  return(head(x))
  }
  </code></pre>