Use ddply within a function and include variable of interest as an argument

前端 未结 4 1148
南方客
南方客 2020-12-30 13:24

I am relatively new to R, and trying to use ddply & summarise from the plyr package. This post almost, but not quite, answers my question. I could use some additional ex

4条回答
  •  悲哀的现实
    2020-12-30 13:39

    I just moved a couple things around in the example function you gave and showed how to get more than one column back out. Does this do what you want?

    myFunction2 <- function(x, y, col){
    z <- ddply(x, y, .fun = function(xx){
                             c(mean = mean(xx[,col],na.rm=TRUE),
                             max = max(xx[,col],na.rm=TRUE) ) })
    return(z)
    }
    
    myFunction2(mtcars, "cyl", "hp")
    

提交回复
热议问题