title: r function error: object 'y' not found
body: <p>I'm really struggling to understand why this function is not working.
  It's just a function to return the coefficients of a simple linear model:</p>
  
  <pre><code>lmfunction &lt;- function(data,y,x,index){
    return(coef(lm(y~x,data=data,subset=index)))
  }
  </code></pre>
  
  <p>Now I create any random dataset:</p>
  
  <pre><code>dataset &lt;- data.frame(x=rnorm(100),y=rnorm(100)*2)
  </code></pre>
  
  <p>When I try to run the function on dataset I get the following error:</p>
  
  <pre><code>lmfunction(dataset,y,x,1:100)
  
  Error in eval(predvars, data, env) : object 'y' not found
  </code></pre>
  
  <p>The y variable definitely exists so I'm not understanding this error.</p>
  
  <p>EDIT:</p>
  
  <p>I restarted my r session and the above code worked. However, if I use it on a real dataset I still run in to problems:</p>
  
  <pre><code>data(iris)
  lmfunction(iris,Sepal.Length,Petal.Length,1:150)
  Error in eval(predvars, data, env) : object 'Sepal.Length' not found
  </code></pre>