user3817704
user3817704

Reputation: 401

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?

Upvotes: 0

Views: 418

Answers (2)

jogo
jogo

Reputation: 12559

Here is a solution with apply() (inspired by @kneijenhuijs )

myfun = function (grate.gshape, Rmax=150, precision=0.1) {
  grate <- grate.gshape[1]; gshape <- grate.gshape[2]
  s = seq(0, Rmax, precision)
  Rc2ds = (1-pgamma(s, rate=grate, shape=gshape))^2 *precision
  sum(Rc2ds, na.rm=TRUE) # intRc2ds
}
apply(mydf, 1, myfun)

you can set the other parameters per "..."-argument of apply()

First I had this solution with mapply():

mydf <- read.table(header=TRUE, text='GRATE GSHAPE
1  0.04    1.0
2  0.08    0.5
3  0.12    2.0')

myfun = function (grate, gshape, Rmax=150, precision=0.1){
  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)
}

mapply(myfun, mydf$GRATE, mydf$GSHAPE)

But then it is hard to set the other parameters (Rmax= and precision=). One other solution can be Vectorize(myfun, ...). The resulting function can work with vectors.

Upvotes: 2

kneijenhuijs
kneijenhuijs

Reputation: 1209

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])})

Upvotes: 2

Related Questions