title: 10 fold Cross Validation - function issues
body: <p>I have created a function to perform 10 fold cross validation on a data set birthwt from library(MASS). The code within the function is doing what i want it to do. However, i want to use the values returned outside of the function but i cant access the mean_mrate variable outside of the function. </p>
  
  <p>My code is: </p>
  
  <pre><code>library(MASS)
  
  tenfold3 = function() {
  
    fold = 10
    end = nrow(birthwt)
    fold_2 = floor(end/fold)
  
    misclasrate=numeric()
  
    for(i in 1:10){
  
      df_i = birthwt[sample(nrow(birthwt)),] # random sort the dataframe birthwt
  
      tester = df_i[1:fold_2,]  # remove first tenth of rows - USE PREDICT ON THIS DATA
      trainer = df_i[-c(1:fold_2),]  # all other than the first tenth of rows - USE GLM ON THIS DATA
  
      #mod = glm(low~age,family=binomial,data=trainer)
  
      mod = glm(low~age+lwt+race+smoke+ptl+ht+ui+ftv,family=binomial,data=trainer)
      ypred = predict(mod,data=tester,type='response')
      ypred = trunc(0.5+predict(mod,data=tester,type='response')) # predicted values
  
      val_df = data.frame(trainer[,1],ypred) 
      names(val_df) = c('train','ypred')
  
      val_df$misclas = (val_df$train == val_df$ypred)
      misclasrate[i] = 1-sum(val_df$misclas) / nrow(val_df)
      mean_mrate = signif(mean(misclasrate),4)
      g = cbind(misclasrate[i],mean_mrate)
      return(mean_mrate)
  
    }
  
  }
  </code></pre>