#Hi guys, i've an issue about a personal exercise in R, i've tried to fill an empty matrix with for #loop but i didn't manage to

#I've this function

classification_perf = function(pred, obs){

overallrate = 1-mean(pred == obs)

cf = table(pred, obs)

sensitivity = cf[2,2]/sum(cf[,2])

specificity = cf[1,1]/sum(cf[,1])

return(c(overall = overallrate,

sens = sensitivity,

spec = specificity))

}

#And this for loop:

vec = c(1,2,3)

errorvec = c()

for(i in 1:length(vec)){

out = knn(train = train[,c(1,2)],

test = test[,c(1,2)],

cl = train[,3],

k = vec[i],

prob = TRUE,

use.all = TRUE)

errorvec[i] = 1-mean(out, test[,3])

}

#Obviously the second codes lead me to fill che errorvec = c(), but i want to as output a #matrix given by the function above "classification_perf" leading me to obtain a 3*3 matrix #with "vec=c(1,2,3)" on rows and "c(overall=overallrate, sens = sensitivity,spec = specificity))" #on cols.

#I've tried in this way bu it gives me an error:

vec = c(1,2,3)

classification = matrix(nrow = 3, ncol =3)

for(j in 1:ncol(classification)){

for(i in 1:length(vec)){

out = knn(train = train[,c(1,2)],

test = test[,c(1,2)],

cl = train[,3],

k = vec[i],

prob = TRUE,

use.all = TRUE)

classification[i,j] = classification_perf(out, test$y)

}

}

#Please help me LOL