[英]compare two columns in R
I have two columns in R, they are columns of 1's and 0's. 我在R中有两列,分别是1和0的列。
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.
这应该不太困难,但是我是使用“ R”编码的新手,因此,非常感谢您的帮助。 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.