title: Cross validation using kNN and RF
body: <p>I have data matrix with 50 column and 150 rows. I wanted to do cross-validation on the dataset. A subset of data look likes below:</p>
  
  <pre><code>&gt;dat&lt;-matrix(c(7,8,5,6,4,5,6,6,1,9,8,5,14,11,15,14,21,24,25,26, 31,34,35,37), nrow=8, ncol=4)
  &gt;colnames(dat)&lt;- c("ABC", "BCD", "CDE", "DEF")
  &gt;dat&lt;-data.frame(dat)
  </code></pre>
  
  <p>First I wanted to replace the first value with <code>NA</code> and impute <code>MV</code> using <code>kNN</code> and <code>Rf</code>. Save the imputed values for each value in column in array for entire dataset and display the result. </p>
  
  <p>Here I did something</p>
  
  <pre><code> library(VIM)
   col &lt;- names(dat)
   das &lt;-dat
   for(name in col){  
      sp &lt;- das[[name]]
      sp&lt;-data.frame(sp)
      ps&lt;-das[,-which(names(das) == name)]
      sp&lt;-cbind(sp,ps)
  for(i in 1:length(sp[,1])){
                 d&lt;-sp[,1]
                 p1&lt;-sp[,2:NCOL(sp)] 
                 d[i]&lt;-NA
                 d&lt;-data.frame(d) 
                 d&lt;-cbind(d,p1)
                 mnt_im&lt;-kNN(d,variable="d", 3)  
                 mnt_m[i]&lt;-mnt_im[i,1]
                 d&lt;-sp[,1]
         }
         #g&lt;-assign(name, mnt_m)
         mnt_m&lt;-data.frame(mnt_m)
         write.csv(mnt_m, "/home/Desktop/result.csv", append=TRUE)
  }
  </code></pre>
  
  <p>After running above code I am getting few warnings</p>
  
  <pre><code>Warning messages:
  1: In write.csv(mnt_m, "/home/Desktop/result.csv", append = TRUE) :
  attempt to set 'append' ignored
  2: In write.csv(mnt_m, "/home/Desktop/result.csv", append = TRUE) :
  attempt to set 'append' ignored
  3: In write.csv(mnt_m, "/home/Desktop/result.csv", append = TRUE) :
  attempt to set 'append' ignored
  4: In write.csv(mnt_m, "/home/Desktop/result.csv", append = TRUE) :
  attempt to set 'append' ignored
  </code></pre>
  
  <p>Moreover, it is not computing the values correctly and even appending in one vector column. I want it for separate column separately. </p>
  
  <p>I think there is something error in imputation.
  Same thing I have to do for every variable. I am not getting what is happening exactly. Can you pinpoint the error in the code that I am doing. Also <code>write.csv</code> is not appending rightly. How can this be done for the entire dataset? I am new to R. How this can be done for <code>kNN</code> as well as <code>rfImpute</code> from the package(<code>randomForest</code>)? </p>