I am trying to create a custom function for batch analysis, at the beginning it is like:
> myfunction <- function(DATA, col1, col2, col3){
> print(class(col3)) #"name"
> print(is.object(col3)) #FALSE
> library(plyr)
> output <- ddply(DATA, .(eval(col1), eval(col2)), summarize, N=sum(eval(col3)),...)
> ...
> }
> myfunction(DATA=df, col1=quote(colA), col2=quote(colB), col3=quote(colC)) #colA[chr], colB[chr], colC[numeric] are column names in dataframe df
but this is what I get:
> Error in eval("col3") : object 'col3' not found
> 10. eval("col3")
> 9. eval(cols[[col]], .data, parent.frame())
> 8. eval(cols[[col]], .data, parent.frame())
> 7. .fun(piece, ...)
> 6. (function (i) { piece <- pieces[[i]] if (.inform) { ...
> 5. loop_apply(n, do.ply)
> 4. llply(.data = .data, .fun = .fun, ..., .progress = .progress, .inform = .inform, .parallel = .parallel, .paropts = .paropts)
> 3. ldply(.data = pieces, .fun = .fun, ..., .progress = .progress, .inform = .inform, .parallel = .parallel, .paropts = .paropts)
> 2. ddply(DATA, .(eval(col1), eval(col2)), summarize, N = sum(eval(col3)))
> 1. myfunction(DATA = df, col1 = quote(colA), col2= quote(colB), col3 = quote(colC))
I dont understand why error comes up until col3 while nothing goes wrong with col1 and col2.
As class(col3) in the custom function show me col3 is a "name", I replace the eval() with get() but it doesn't work.
Can anyone tell me how to get the object behind the name col3?
Or am I in the wrong way from the start and need to change my mindset allover?
plyris a bit outdated package. It may be easier to work withdplyrplyr