Q:

How to apply my own function with r::apply()

I have following dataframe
   GRATE GSHAPE
1  0.04    1.0
2  0.08    0.5
3  0.12    2.0

I want to compute a new column COL by following function
myfun = function (Rmax=150, precision=0.1, grate, gshape){
  s = seq(0,Rmax,precision)
  R = pgamma(s, rate=grate, shape=gshape)
  Rc = 1-R
  Rc2 = Rc^2
  Rc2ds = Rc2*precision
  intRc2ds = sum(Rc2ds, na.rm=TRUE)
  return(intRc2ds)
}

This is not working correctly:
mydf$COL = apply(mydf, 2, myfun, grate=mydf$GRATE, gshape=mydf$GSHAPE)

How should I do this?

A:

What do you wish to accomplish exactly? Making it run across columns doesn't make sense, because then you never have both a grate and gshape, but only one at a time.
If you want to make it run across rows (so that you get an answer for the combinations of grate and gshape presented in your rows), this code works:
mydf$COL = apply(mydf, 1, function(x) {myfun(grate=x[1], gshape=x[2])})

