简体   繁体   中英

False positive and false negative without loop in R

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM