简体   繁体   中英

compare two columns in R

I have two columns in R, they are columns of 1's and 0's.

  Score Predict
     1       1
     1       0
     0       1
     1       1
     0       1
     1       0
     1       1
     1       1
     1       1
     0       1
     0       0
     0       0
     0       0
     1       1
     1       1
     1       1

I need to write a function that compares each column and gets an average of how many times the predicted column is the same as the score column. This shouldn't be too difficult but I am new with 'R' coding so any help is greatly appreciated. Thanks!

Comment to answer:

compares each column... predicted column is the same as the score column

Score == Predict # or with(df, Score == Predict)

and gets an average

mean(Score == Predict) # with(df, Score == Predict)

If your data is

 set.seed(123)
 df <- data.frame(a = rbinom(10, 1, 0.5), b = rbinom(10, 1, 0.75) )

Then

sum(df$a == df$b)/nrow(df) 
[1] 0.9
bdf <- data.frame(Score = rbinom(10, 1, 0.5), Predict = rbinom(10, 1, 0.5))
sum(with(bdf, Score == Predict))/nrow(bdf)

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