Search code examples
rtruthtable

Deriving a contingency truth table from two columns with yes and no values in R


I've been trying to manipulate my data in R to something similar here How to Find False Positive Prediction Count using R Script , but difficult doing it owing to lack of minimal example. My dataframe (called info) is like this:

obs sim no no no no no no no yes yes yes yes yes yes no no no no no no no no no yes yes NA yes no yes yes yes yes yes yes yes

What I would like to obtain is a truth table that drops any row with NA in either column, with the result as follows:

   obs  sim 
     yes no  
yes    6 2  
 no    1 7 

Solution

  • We can use complete.cases to create a logical index that will give FALSE if there is any NA in a row to subset the rows and then apply table

    table(info[complete.cases(info),])
    #    sim
    #obs    no yes
    #  no    7   2
    #   yes  1   6
    

    Or with na.omit

    table(na.omit(info))
    

    data

    info <- structure(list(obs = c("no", "no", "no", "no", "yes", "yes", 
     "yes", "no", "no", "no", "no", "yes", NA, "no", "yes", "yes", 
     "yes"), sim = c("no", "no", "no", "yes", "yes", "yes", "no", 
     "no", "no", "no", "no", "yes", "yes", "yes", "yes", "yes", "yes"
     )), class = "data.frame", row.names = c(NA, -17L))