Performing machine learning I've written the code below to do some work on machine learning in R. I'm not overly happy with some bits of it, and I suspect I could improve it quite a bit. Bits I'm specifically interested in looking at are how to deal with appending to vectors in the loop, and whether I can combine all of the functions kf.knn, kf.svm etc into one function with various arguments.

library(class)
library(nnet)
library(epibasix)
library(CVThresh)
library(e1071)

df &lt;- read.table("semeion.data.data")
df$digit &lt;- replicate(nrow(df), 42)

df$digit[which(as.logical(df$V266), arr.ind=TRUE)] = 9
df$digit[which(as.logical(df$V265), arr.ind=TRUE)] = 8
df$digit[which(as.logical(df$V264), arr.ind=TRUE)] = 7
df$digit[which(as.logical(df$V263), arr.ind=TRUE)] = 6
df$digit[which(as.logical(df$V262), arr.ind=TRUE)] = 5
df$digit[which(as.logical(df$V261), arr.ind=TRUE)] = 4
df$digit[which(as.logical(df$V260), arr.ind=TRUE)] = 3
df$digit[which(as.logical(df$V259), arr.ind=TRUE)] = 2
df$digit[which(as.logical(df$V258), arr.ind=TRUE)] = 1
df$digit[which(as.logical(df$V257), arr.ind=TRUE)] = 0

data &lt;- df[,c(0:256,267)]

squareTable &lt;- function(x,y) {
    x &lt;- factor(x)
    y &lt;- factor(y)

    commonLevels &lt;- sort(unique(c(levels(x), levels(y))))

    x &lt;- factor(x, levels = commonLevels)
    y &lt;- factor(y, levels = commonLevels)

    table(x,y)

}

kf.knn &lt;- function(data, k, nearest)
{
  N &lt;- nrow(data)
  data &lt;- data[sample(1:N),]

  folds.index &lt;- cvtype(N, cv.bsize=1, cv.kfold=k, FALSE)$cv.index

  total = 0

  for (i in 1:k)
  {
    test &lt;- data[folds.index[i,], 0:256]
    test.labels &lt;- data[as.array(folds.index[i,]), 257]

    rest &lt;- as.array(folds.index[-i,])

    train &lt;- data[rest, 0:256]
    train.labels &lt;- data[rest, 257]

    knnpredict &lt;- knn(train, test, train.labels, nearest)
    t &lt;- table(as.factor(test.labels), as.factor(knnpredict))

    kap &lt;- epiKappa(t)
    total &lt;- total + kap$kappa
  }

return(total / k)
}

kf.nnet &lt;- function(data, k, hidden)
{
  N &lt;- nrow(data)
  data &lt;- data[sample(1:N),]

  folds.index &lt;- cvtype(N, cv.bsize=1, cv.kfold=k, FALSE)$cv.index

  total = 0

  for (i in 2:k)
  {
    test &lt;- data[folds.index[i,], 0:256]
    test.labels &lt;- data[as.array(folds.index[i,]), 257]

    rest &lt;- as.array(folds.index[-i,])

    train &lt;- data[rest, 0:256]
    train.labels &lt;- data[rest, 257]

    train$label &lt;- as.factor(train.labels)


    nnetwork &lt;- nnet(label ~ ., data=train, size=hidden, MaxNWts = 10000, maxit=200, linout=TRUE)
    nnetpredict &lt;- predict(nnetwork, test, "class")

    table &lt;- squareTable(as.factor(test.labels), as.factor(nnetpredict))

    print(table)
    kap &lt;- epiKappa(table)
    total &lt;- total + kap$kappa
  }

return(total / k)
}

kf.svm &lt;- function(data, k)
{
  N &lt;- nrow(data)
  data &lt;- data[sample(1:N),]

  folds.index &lt;- cvtype(N, cv.bsize=1, cv.kfold=k, FALSE)$cv.index

  total = 0

  for (i in 2:k)
  {
    test &lt;- data[folds.index[i,], 0:256]
    test.labels &lt;- data[as.array(folds.index[i,]), 257]

    rest &lt;- as.array(folds.index[-i,])

    train &lt;- data[rest, 0:256]
    train.labels &lt;- data[rest, 257]

    train$label &lt;- as.factor(train.labels)

    svmmodel &lt;- svm(label ~ ., data=train, cost=8, gamma=0.001, cross=2)
    svmpredict &lt;- predict(svmmodel, test)

    t &lt;- table(as.factor(test.labels), as.factor(svmpredict))
    print(t)
    kap &lt;- epiKappa(t)
    total &lt;- total + kap$kappa
  }

return(total / k)
}

result &lt;- vector("numeric", 1)
n &lt;- vector("numeric", 1)


for (i in 2:10)
{
  avg &lt;- kf.nnet(data, 5, i)
  result &lt;- append(result, avg)
  n &lt;- append(n, i)
  cat("!!!!!!!", i, avg, "\n")
}

result &lt;- result[2:length(result)]
n &lt;- n[2:length(n)]

df &lt;- data.frame(kappa = result, n=n)
plot(df$n, df$res)

print(df)
You can avoid appending to vectors (which can cause re-allocation of space and can considerably slow things down in principle; though in your case of only a length 10 vector that shouldn't be noticeable) if you allocate them to the needed size initially and then assign within them.

result &lt;- vector("numeric", 10)
# or even: result &lt;- numeric(10)
n &lt;- vector("numeric", 10)

for (i in 2:10)
{
  avg &lt;- kf.nnet(data, 5, i)
  result[i] &lt;- avg
  n[i] &lt;- i
  cat("!!!!!!!", i, avg, "\n")
}


I'm not clear what you are asking about combing function; you can add an additional argument which is a character vector which then the code just has a series of if/elseif's checking that parameter to see which algorithm to use, if that is what you mean.