Student T Student T - 1 month ago 18
R Question

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?

Answer

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))
Comments