title: I have a function which scores my data as 1 or 0(right or wrong) for MCQ responses(A,B,C,D). I want NA to be scored as NA not as 0
body: <p>I have a column called 'KEY' which stores the correct response for each MCQ question. I also have the data set which contains scored responses such as A,B,C,D. <a href="https://imgur.com/a/DluQZtJ" rel="nofollow noreferrer">This</a>(notice how it has some responses which are NA or --) is how my data looks like. This is how my <a href="https://imgur.com/a/mGuQivz" rel="nofollow noreferrer">Key</a> looks like.</p>
  <p>I am using <a href="https://github.com/philchalmers/mirt/blob/master/R/key2binary.R" rel="nofollow noreferrer">this function</a> to score the data:-</p>
  <pre><code>  key2binary &lt;- function (fulldata, key, score_missing = FALSE){
      if(missing(fulldata)) missingMsg('fulldata')
      if(missing(key)) missingMsg('key')
      if(is.vector(key)) key &lt;- matrix(key)
      if (ncol(fulldata) != nrow(key)) stop(&quot;Key is not the correct length.\n&quot;, call.=FALSE)
      colname &lt;- colnames(fulldata)
      X &lt;- matrix(0L, nrow(fulldata), ncol(fulldata))
      colnames(X) &lt;- colname
      for(i in 1L:ncol(X)){
        if(all(is.na(key[i,]))) next
        X[,i] &lt;- fulldata[,i] %in% key[i,] + 0L
      }
      if(!score_missing)
        X[is.na(fulldata)] &lt;- NA
      X
    }
  </code></pre>
  <p>The issue is that it also scores responses which are NA as 0. I want responses which are NA to be scored as NA only.</p>
  <pre><code>resp &lt;- as.data.frame(matrix(c(
    &quot;B&quot;,&quot;B&quot;,&quot;NA&quot;,&quot;D&quot;,&quot;E&quot;,
    &quot;B&quot;,&quot;A&quot;,&quot;D&quot;,&quot;NA&quot;,&quot;E&quot;,
    &quot;B&quot;,&quot;A&quot;,&quot;D&quot;,&quot;C&quot;,&quot;E&quot;,
    &quot;D&quot;,&quot;D&quot;,&quot;D&quot;,&quot;C&quot;,&quot;E&quot;,
    &quot;B&quot;,&quot;C&quot;,&quot;A&quot;,&quot;D&quot;,&quot;A&quot;), ncol=5, byrow=TRUE))
  
  key &lt;- c(&quot;B&quot;, &quot;D&quot;, &quot;D&quot;, &quot;C&quot;, &quot;E&quot;)
  
  key2binary(resp, key)
  </code></pre>
  <p>This returns NA as 0 . But i want NA to be returned as NA</p>