title: How to give a score to each dataframe value based on scorecard in another database?
body: <p>I'm trying to create a scorecard for the <code>values</code> relative to the <code>scorecard</code> (both below).</p>
  <pre><code>values &lt;- data.frame(A= c(-200,-150,-100,0,100),
                       B= c(100,0,-101,-201,-300),
                       C= c(-400,400,500,-500,250),
                       D= c(NA,NA,-1000,-1000,-1000),
                       E= c(1000,1000,1,-1000,-2000))
  
  scorecard &lt;- data.frame(Names = c(&quot;A&quot;,&quot;B&quot;,&quot;C&quot;,&quot;D&quot;,&quot;E&quot;), 
                          &quot;Score5&quot; = c(-100,-200,-300,-400,-500))
  
  values
       A    B    C     D     E
  1 -200  100 -400 -1000  1000
  2 -150    0  400 -1000  1000
  3 -100 -101  500 -1000     1
  4    0 -201 -500 -1000 -1000
  5  100 -300  250 -1000 -2000
  </code></pre>
  <p>If A's value &lt; -100 (i.e. <code>scorecard[1,2]</code>), the scorecard dataframe should say 5, otherwise it should say 0. And I'd like to do this for all of A, B, C, D and E in one dataframe. The desired output is:</p>
  <pre><code>#  A B C  D E
  #1 5 0 5 NA 0
  #2 5 0 0 NA 0
  #3 0 0 0  5 0
  #4 0 5 5  5 5
  #5 0 5 0  5 5
  </code></pre>
  <p>I've tried the following - which required the packaged xts: <code>install.packages(&quot;xts&quot;)</code> but I didn't quite get there.</p>
  <pre><code>pointsfunction &lt;- function(value)  {
    points &lt;- c()
    for(i in names) {
      index = which(colnames(value)==i)
      data_start &lt;- which(!is.na(value))[1]
      points[1:(data_start -1)] &lt;- NA
      for(a in (data_start):(length(value))) {
        if(value[a] &lt; scorecard[index, 2]) {
          points[a] &lt;- -5
        } else {
          points[a] &lt;- 0
        }
      }
    }
    points &lt;- reclass(points, value)
    return(points)
  }
  
  scorecardpoints &lt;- as.data.frame(lapply(values, pointsfunction))
  </code></pre>
  <p>I got the following error:</p>
  <blockquote>
  <p>Error in if (value[a] &lt; scorecard[index, 2]) { :    argument is of
  length zero Called from: FUN(X[[i]], ...)</p>
  </blockquote>
  <p>Any ideas?</p>