etuo
etuo

Reputation: 155

R check if values in row are equal

I have data in the following format:

image1

what I want to do is check in each row whether the value in pred is = to the one in obs

so in the example above 3 out of 5 are equal and the accuracy would be 60% ?

How can I do that?

Screenshot of code:

image2

Upvotes: 0

Views: 4035

Answers (1)

LachlanO
LachlanO

Reputation: 1162

It looks like your data frame is called "prediction".

The == operation takes each element if the vectors are of equal lengths and compares them, returning a TRUE or FALSE. When performing arithmetic on TRUEs and FALSEs they're treated like 1s and 0s respectively. As such the mean of this == comparison will give you what you want.

mean(prediction[["pred"]]==prediction[["obs"]])

Upvotes: 1

Related Questions