Home > Software design >  R: how to get the value or object from a name in a custom function?
R: how to get the value or object from a name in a custom function?

Time:11-21

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?

CodePudding user response:

It may be easier to use dplyr instead of plyr. Here, is one way to change the function

myfunction <- function(DATA, col1, col2, col3){

   
    plyr::ddply(DATA, c(col1, col2),
         .fun = function(.data) c(N = sum(.data[[col3]], na.rm = TRUE)))

 }

-testing

> myfunction(mtcars, "cyl", "vs", "mpg")
 cyl vs     N
1   4  0  26.0
2   4  1 267.3
3   6  0  61.7
4   6  1  76.5
5   8  0 211.4
# outside the function
> plyr::ddply(mtcars, c("cyl", "vs"), summarize, N = sum(mpg))
  cyl vs     N
1   4  0  26.0
2   4  1 267.3
3   6  0  61.7
4   6  1  76.5
5   8  0 211.4
  • Related