I have the following dataframe (simplified example)
y.true yhat
1 U E
2 E U
3 U E
4 E U
5 E U
6 U E
7 E U
8 E E
9 U U
10 E E
I need to calculate the number of false negative (y.true==U,yhat==E
) and false positive (y.true==E,yhat==U
) where E=0, U=1 in my case. Of course I could right a for loop of the type:
FP<-0
FN<-0
for (i in 1:dim(df.b)[1]) {
if (df.b[i,1]=='U' & df.b[i,2]=='E') {
FN<-FN+1
}
else if (df.b[i,1]=='E' & df.b[i,2]=='U') {
FP<-FP+1
}
}
However: is there a more efficient way to get this task done using logical indexing instead of a loop?
If df.b
is your data.frame, you don't need to use a for-loop at all.
FP <- sum(df.b$y.true == 'U' & df.b$yhat == 'E')
FN <- sum(df.b$y.true == 'E' & df.b$yhat == 'U')