# Difference between lazy and substitute in R

I'm trying to use the `lazyeval` package to create non-standard evaluation in R, but was confused about what's the difference between `substitute` and `lazy`.

``df <- data.frame(col1 = runif(10), col2 = runif(10))> dfcol1 col21 0.54959138 0.89267782 0.99857207 0.96495923 0.26451336 0.92430964 0.98755113 0.71558825 0.84257525 0.59183876 0.20692997 0.58759447 0.44383744 0.58392358 0.44014903 0.10060809 0.49835993 0.763761910 0.07162048 0.3155483``

I first created a function to take a data frame and two column names and return a column that is the sum of the two columns. `substitute` and `eval` seem to work just fine.

``SubSum <- function(data, x, y) {exp <- substitute(x+y)r <- eval(exp, data)return(cbind(data, data.frame(sum=r)))}> SubSum(df, col1, col2)col1 col2 sum1 0.54959138 0.8926778 1.44226922 0.99857207 0.9649592 1.96353123 0.26451336 0.9243096 1.18882294 0.98755113 0.7155882 1.70313945 0.84257525 0.5918387 1.43441406 0.20692997 0.5875944 0.79452447 0.44383744 0.5839235 1.02776108 0.44014903 0.1006080 0.54075709 0.49835993 0.7637619 1.262121810 0.07162048 0.3155483 0.3871688``

I then tried to create a function with `lazy` and `lazy_eval`, but it didn't work.

``require(lazyeval)LazySum <- function(data, x, y) {exp <- lazy(x+y)r <- lazy_eval(exp, data)return(cbind(data, data.frame(sum=r)))}> LazySum(df, col1, col2)Error in eval(expr, envir, enclos) : object 'col1' not found``

After some trial and error, this snippet seems to work.

``LazySum <- function(data, x, y) {exp <- interp(～x + y, x=lazy(x), y=lazy(y))r <- lazy_eval(exp, data)return(cbind(data, data.frame(sum=r)))}``

Basically I had to build the lazy expression myself using `interp`.

You were pretty close. read `?lazy` especially the examples to understand the changes I made to your code

``````require(lazyeval)
set.seed(357)
df <- data.frame(col1 = runif(10), col2 = runif(10))
LazySum <- function(data, sum=x+y) {
exp <- lazy(sum) #giving lazy a named arguement
r <- lazy_eval(exp, data)

return(cbind(data, data.frame(sum=r)))
}

LazySum(df, col1+col2)
``````