title: How to loop and keep the output of each loop in r
body: <p>I have some code as per below. All I would like to do is to run this code for each of the columns in my dataframe and then keep the output somewhere (?another dataframe with associated column names).
  I've tried creating a loop but keep on getting the error:</p>
  
  <p>Error in (finalset_row_number[i] - 1):(finalset_row_number[i] + 1) : 
    NA/NaN argument. </p>
  
  <p>I know why the error happens but I dont want it to stop the other values from being created. The code is as follows:</p>
  
  <pre><code>    df1 &lt;- read.delim(file.choose(),header=TRUE)
  
  
      #Take the control samples and average each row for three columns excluding the first two columns- add the per row means to the data frame
      df$Means &lt;- rowMeans(df[,30:32]) 
      RowVar &lt;- function(x) {rowSums((x - rowMeans(x))^2)/(dim(x)[2] - 1)}
      df$sd=sqrt(RowVar(df[,c(30:32)]))
  
      #Get a Z score by dividing the test sample count at each locus by the average for the control samples and divide everything by the st dev for controls at each locus.
      {
      df$ZScore &lt;- (df[,35]-df$Means)/(df$sd)
  
      ######################################### QUARTILE FILTER ###########################################################
      alpha=1.5
      numberofControls = 3
      UL = median(df$ZScore, na.rm = TRUE) + alpha*IQR(df$ZScore, na.rm = TRUE)
      LL = median(df$ZScore, na.rm = TRUE) - alpha*IQR(df$ZScore, na.rm = TRUE)
  
      #Copy the Z score if the score is &gt; or &lt; a certain number, i.e. LL or UL.
      Zoutliers &lt;- which(df$ZScore &gt; UL | df$ZScore &lt; LL)
      df$Zoutliers &lt;- ifelse(df$ZScore &gt; UL |df$ZScore &lt;LL ,1,-1)
      tempout = ifelse(df$ZScore[Zoutliers] &gt; UL,1,-1)
  
      ######################################### Three neighbour Isolation filter ##############################################################################
      finalSeb=c()
      for(i in 2:(length(Zoutliers)-1)){
       j=Zoutliers[i]
       if(sum(ifelse((j-1) == Zoutliers,1,0)) &gt; 0 &amp; tempout[i] ==  tempout[i-1] &amp; sum(ifelse((j+1) == Zoutliers,1,0)) &gt; 0 &amp; tempout[i] ==  tempout[i+1]){
         finalSeb = c(finalSeb,i)
       }  
      }
      finalset_row_number = Zoutliers[finalSeb]
      #View(finalset_row_number)
      p_seq = rep(0,nrow(df))
      for(i in 1:length(finalset_row_number)){
       p_seq[(finalset_row_number[i]-1):(finalset_row_number[i]+1)] = median(df$ZScore[(finalset_row_number[i]-1):(finalset_row_number[i]+1)])
      }
      sum(p_seq !=0)
      CopyNumberCount
      nrow(SU)
      nrow(as.data.frame(finalset_row_number))
      }
  </code></pre>
  
  <p>I tried using try as follows:</p>
  
  <pre><code>for(i in 3:ncol(df)) try(
      df$ZScore &lt;- (df[,i]-df$Means)/(df$sd)
  </code></pre>
  
  <p>etc (with brackets closed after the final }) but this just gives me a few of the same error messages but without an error for the columns where it has worked. Ideally if I get this error message the final output of        nrow(as.data.frame(finalset_row_number)) just being set to zero. Can anyone help with this loop?</p>