Reputation: 815
I have one dataframe (df1) that contains predicted values, which is coded as -1 and 1 to represent a predicted decrease or increase. 0 represents no predicted change (note: I do not need this as I am only interested in predicted increases or decreases). The second dataframe is in the same format, but represents the observed changes (i.e. in actuality whether something did actually increase or decrease). The rows and columns match up, so that df1[1,1] would represent the predicted value and df2[1,1] would represent the observed response for the same observation.
What I would like to do is get a count or total of three things: (i) whether the prediction was correct (i.e whether -1 and 1 values in df1 match up with the same value in the corresponding cell in df2); (ii) whether predicted values actually observed no change (i.e. whether -1 and 1 values in df1 have a 0 in the corresponding cell in df2) and (iii) whether predicted values was completely incorrect (i.e. whether -1 and 1 values in df1 have the opposite value in the corresponding cell in df2).
df1 <- as.data.frame(matrix(sample(-1:1, 40*2, replace=TRUE), ncol=8))
df2 <- as.data.frame(matrix(sample(-1:1, 40*2, replace=TRUE), ncol=8))
Upvotes: 0
Views: 55
Reputation: 7796
You can just set up a few logical checks
# (i)
sum(df1 == df2 & df1 != 0)
# (ii)
sum(df1 != df2 & df2 == 0)
# (iii)
sum(df1 != df2 & df1 != 0 & df2 != 0)
Upvotes: 1