confusionMatrix r accuracy per class code example

Example: classification accuracy metric in r

ClassificationAccuracyMetric <- function (Actual, Predicted){
  TP <- 0 # Actual normal point + Predicted normal point
  FN <- 0 # Actual normal point + Predicted fault point
  FP <- 0 # Actual fault point + predicted normal point
  TN <- 0 # Actual fault point + Predicted fault point
  for (i in 1:length(Actual)){
    if (Actual[i]==0 & Predicted[i]==0){
      TP=TP+1
    }
    if (Actual[i]==0 & Predicted[i]==1){
      FN=FN+1
    }
    if (Actual[i]==1 & Predicted[i]==0){
      FP=FP+1
    }
    if (Actual[i]==1 & Predicted[i]==1){
      TN=TN+1
    }
  }

  return ((TN + TP)/(TN + TP + FN + FP))
}

print(ClassificationAccuracyMetric( c(1,1,0,0,1,0,1,1,1,1), c(1,1,0,0,1,0,1,1,1,0) ))