SmallChess
SmallChess

Reputation: 8146

How to lapply to this function?

This is the function declaration (fOptions::GBSGreeks) I want to apply a list to:

function (Selection = c("Delta", "Theta", "Vega", "Rho", "Lambda", 
    "Gamma", "CofC"), TypeFlag = c("c", "p"), S, X, Time, r, 
     b, sigma)

I want to try various Selection while fixing all other parameters. I tried:

library(fOptions)

greeks <- lapply(Selection=list('Delta', 'Gamma', 'Vega', 'Theta', 'Rho'),
             FUN=fOptions::GBSGreeks, TypeFlag='p', S=100.0, X=100.0, Time=1.0, r=0.05, b=0, sigma=0.05)

but the result is just a single element:

[[1]]
[1] -0.4661285

Q: How to do lapply in my example?

Upvotes: 1

Views: 64

Answers (1)

Jandre Marais
Jandre Marais

Reputation: 328

Try

greeks <- lapply(list('Delta', 'Gamma', 'Vega', 'Theta', 'Rho'),
             function(a) GBSGreeks(Selection = a,
                                   TypeFlag='p', 
                                   S=100.0, X=100.0, 
                                   Time=1.0, r=0.05, b=0, sigma=0.05))

Upvotes: 1

Related Questions