title: Rearranging data to a matrix for contour plot
body: <p>I have this data: </p>
  
  <pre><code>df = structure(list(vint1 = c(10L, 1L, 1L, 1L, 7L, 10L, 7L, 1L, 8L, 
  3L, 7L, 9L, 1L, 5L, 5L), vint2 = c(3L, 4L, 4L, 1L, 3L, 4L, 6L, 
  5L, 6L, 3L, 10L, 4L, 1L, 8L, 8L), vnum1 = c(-1.17289752533732, 
  -0.559339864207054, -0.595443000061417, -0.396535659893954, 0.788141517690765, 
  -0.655833840195406, -2.26371235489487, -1.34850886354386, -0.0218824069117636, 
  0.554324892501117, 2.37117531121636, 0.248289029610446, -1.21942427707135, 
  -1.4366686196659, -2.64837580107992)), .Names = c("vint1", "vint2", 
  "vnum1"), class = "data.frame", row.names = c(NA, -15L))
  </code></pre>
  
  <p>when I give following command on R prompt, it works: </p>
  
  <pre><code>with(df[vint1==10 &amp; vint2==3,], mean(vnum1))  
  </code></pre>
  
  <p>[1] -1.172898</p>
  
  <p>But following function give incorrect result: </p>
  
  <pre><code>testfn = function(df2, a,b,c)
  {
      with(df2[df2[[a]]==10 &amp; df2[[b]]==3,], mean(df2[[c]]))
  }
  </code></pre>
  
  <p>when I give the command: </p>
  
  <pre><code>testfn(df,1,2,3)
  
  [1] -0.5571128
  </code></pre>
  
  <p>This value is mean of all entries from 3rd column (vnum1) </p>
  
  <pre><code>mean(df$vnum1)
  [1] -0.5571128
  </code></pre>
  
  <p>Following function also does not work: </p>
  
  <pre><code>testfn = function(df2, name1,name2,name3)
  {
      with(df2[name1==10 &amp; name2==3,], mean(name3))
  }
  </code></pre>
  
  <p>If I use: with(df2[df2$name1==10 &amp; df$name2==3,], mean(df2$name3))
  I get the error: 
  argument is not numeric or logical: returning NA</p>
  
  <p>I have to subsequently allot this value to a create a matrix, which can used to create a contour plot: </p>
  
  <pre><code>mycontour = function(df2, a,b,c)
  {
      mymat = matrix(0,10,10)
      for(i in 1:10)   for(j in 1:10)
          mymat[i,j]= with(df2[df2[[a]]==i &amp; df2[[b]]==j,], mean(df2[[c]]))
      filled.contour(mymat)
  }  
  </code></pre>
  
  <p>This should create a contour plot with two ordered number (1:10) variables as the x and y axis, and contour accoring to the third (numeric) variable (means of values corresponding to 2 factor levels).  How can I solve this? Thanks for your help. </p>