R Syntax != Doubt

Hi all,

I came across a code syntax that I have never seen before, any help appreciated.

mean(test.purchase != predicted.purchase)  #input
[1] 0.016 #output

test.purchase and predicted.purchase are factor columns with two levels - yes and no.
What is != doing here, and how is it giving a numeric output ?


Numeric output is caused by mean function, not the != operator.

The operator just checks uf prediction matches actual or not, and return a boolean vector. Now, when that is passed to the function, which is really for numeric values only, attempts for coercion. Since TRUE and FALSE can be converted to 1 and 0 respectively, the attempt succeeds and then it's a numeric vector of 1's and 0's. And the output you got is the average of that vector, and it equals the proportion of 1 in the vector.

Hope this helps.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.