Object not found error with ddply inside a function

后端 未结 5 720
别跟我提以往
别跟我提以往 2020-11-30 01:58

This has really challenged my ability to debug R code.

I want to use ddply() to apply the same functions to different columns that are sequentially nam

5条回答
  •  北海茫月
    2020-11-30 02:21

    The problem lies in the code of the plyr package itself. In the summarize function, there is a line eval(substitute(...),.data,parent.frame()). It is well known that parent.frame() can do pretty funky and unexpected stuff. T

    he solution of @James is a very nice workaround, but if I remember right @Hadley himself said before that the plyr package was not intended to be used within functions.

    Sorry, I was wrong here. It is known though that for the moment, the plyr package gives problems in these situations.

    Hence, I give you a base solution for the problem :

    myFunction <- function(x, y){
        NewColName = "a"
        z = aggregate(x[NewColName],x[y],mean,na.rm=TRUE)
        return(z)
    }
    > myFunction(df,sv)
      b   a
    1 0 1.5
    2 1 3.5
    

提交回复
热议问题