I have a data.frame called dat.
colnames(dat)
[1] "variable" "weight"
When I run aggregate(weight ~ variable, dat, sum)
the function runs without error and returns the values I would expect.
However, when I embed aggregate()
within a custom function as follows:
bins <- function(df, var, wt, n) {
tmp <- aggregate(wt ~ var, df, sum)
####################
other code not shown
####################
return(tmp)
}
And then run out <- bins(df=dat, var=variable, wt=weight, n=5)
, I get the following error message:
Error in eval(expr, envir, enclos) : object 'weight' not found
I tried using with()
as well without success.