title: In R: dcast in function, pass column names (again!)
body: <p>Given a df in semi-long format with id variables <code>a</code> and <code>b</code> and measured data in columns <code>m1</code>and <code>m2</code>. The type of data is specified by the variable <code>v</code> (values var1 and var2). </p>
  
  <pre><code>set.seed(8)
  
  df_l &lt;- 
    data.frame(
      a = rep(sample(LETTERS,5),2),
      b = rep(sample(letters,5),2),
      v = c(rep("var1",5),rep("var2",5)),
      m1 = sample(1:10,10,F),
      m2 = sample(20:40,10,F)) 
  </code></pre>
  
  <p>Looks as:</p>
  
  <pre><code>   a b    v m1 m2
  1  W r var1  3 40
  2  N l var1  6 32
  3  R a var1  9 28
  4  F g var1  5 21
  5  E u var1  4 38
  6  W r var2  1 35
  7  N l var2  8 33
  8  R a var2 10 29
  9  F g var2  7 30
  10 E u var2  2 23
  </code></pre>
  
  <p>If I want to make a wide format of values in <code>m1</code> using id <code>a</code> as rows and values in <code>v1</code>as columns I do:</p>
  
  <pre><code>&gt; reshape2::dcast(df_l, a~v, value.var="m1")
    a var1 var2
  1 E    4    2
  2 F    5    7
  3 N    6    8
  4 R    9   10
  5 W    3    1
  </code></pre>
  
  <p>How do I write a function that does this were arguments to <code>dcast</code> (row, column and value.var) are supplied as arguments, something like:</p>
  
  <pre><code>fun &lt;- function(df,row,col,val){
    require(reshape2)
    res &lt;-
      dcast(df, row~col, value.var=val)
    return(res)
  }
  </code></pre>
  
  <p>I checked SO <a href="https://stackoverflow.com/questions/2641653/pass-a-data-frame-column-name-to-a-function?lq=1">here</a> and <a href="https://stackoverflow.com/questions/28324212/r-pass-column-name-as-argument-to-function-using-dplyrfilter-and-in?lq=1">here</a> to try variations of <code>match.call</code> and <code>eval(substitute())</code> in order to "get" the arguments inside the function, and also tried with the lazyeval package. No succes. </p>
  
  <p>What am I doing wrong here ? How to get dcast to recognize variable names?  </p>