hpmurg
hpmurg

Reputation: 107

How can I let my program run 100 times and save my results in a dataframe?

I have a program which divides my data in the first step into train and test set. After that, a decision tree is built and I receive a confusion matrix.

What I want to do now is, repeat these steps ( divide train and test, decision tree and confusion matrix) for 100 times, so I always get a different train and test data set.

What I want is to get a resulting data frame with the accuracy, the sensitivity and the specifity of the confusion matrix.

The accuracy, sensitivity and the specifity are saved in vectors:

overall.accuracy <- format(overall['Accuracy'] * 100, nsmall =2, digits = 2)
overall.sensitivity <- format(cm$byClass['Sensitivity']* 100, nsmall =2, digits = 2)
overall.specificity <- format(cm$byClass['Specificity']* 100, nsmall =2, digits = 2)

My desired output would be sth like this:

> result_df

    accuracy      sensitivity    specifity 
1    30.22%          95.12%       30.23%
2    34.10%          80.12%       27.76%
3    31.56%          85.78%       28.98%
.       
.
.
100  32.33%          87.34%       29.45%

I could use replicate() but I am not familiar with this function and I don't know how to save the accuracy, sensitivity and the specificty of each cylce in a data frame.

Upvotes: 0

Views: 99

Answers (1)

Beavis
Beavis

Reputation: 498

The easiest way to do this would be to use a for loop, ie.

result_df<-matrix(ncol=3,nrow=100)
colnames(result_df)<-c("Acc","Sens","Spec")

for (i in 1:100)
{
YOUR CODE HERE
result_df[i,1] <- format(overall['Accuracy'] * 100, nsmall =2, digits = 2)
result_df[i,2] <- format(cm$byClass['Sensitivity']* 100, nsmall =2, digits = 2)
result_df[i,3] <- format(cm$byClass['Specificity']* 100, nsmall =2, digits = 2)
}

Upvotes: 2

Related Questions