I have a program where the following code works fine: df.opt$Delta <- apply(df.opt, 1, function(x) EuropeanOption(x["CallPutString"], x["UnderlyingPrice"], x["StrikePrice"], 0, interest.rate, x["FractionalExpiration"], x["Vol"])[[2]]) However, the code below fails: #df.opt$Delta <- apply(df.opt, 1, function(x) EuropeanOption(x$CallPutString, #x["UnderlyingPrice"], x["StrikePrice"], 0, interest.rate, x["FractionalExpiration"], #x["Vol"])[[2]]) Here df.opt is a data frame and European Option is a function in the package RQuantLib. The only difference between the two pieces of code is that a column of the data frame is accessed using "$" in the first case and brackets in the second case. Sorry if this is a trivial question, but could somebody tell me the reason for this behavior? FS
with apply loop, x is a vector you are indexing in the first sample code with its names. so it failed when you tried indexing it with "$" which is dedicated to data frames. Fernando Saldanha a ??crit :>I have a program where the following code works fine: > >df.opt$Delta <- apply(df.opt, 1, function(x) >EuropeanOption(x["CallPutString"], x["UnderlyingPrice"], >x["StrikePrice"], 0, interest.rate, x["FractionalExpiration"], >x["Vol"])[[2]]) > >However, the code below fails: > >#df.opt$Delta <- apply(df.opt, 1, function(x) >EuropeanOption(x$CallPutString, #x["UnderlyingPrice"], >x["StrikePrice"], 0, interest.rate, x["FractionalExpiration"], >#x["Vol"])[[2]]) > >Here df.opt is a data frame and European Option is a function in the >package RQuantLib. >The only difference between the two pieces of code is that a column of >the data frame is accessed using "$" in the first case and brackets in >the second case. > >Sorry if this is a trivial question, but could somebody tell me the >reason for this behavior? > >FS > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > > >
$ can be used with data frames but not arrays and apply is not sending a data frame to your function. For example, try this: apply(df.opt, 1, is.data.frame) By the way, in addition the difference you mention there are a bunch of # marks in your second statement. On 1/19/06, Fernando Saldanha <fsaldan1 at gmail.com> wrote:> I have a program where the following code works fine: > > df.opt$Delta <- apply(df.opt, 1, function(x) > EuropeanOption(x["CallPutString"], x["UnderlyingPrice"], > x["StrikePrice"], 0, interest.rate, x["FractionalExpiration"], > x["Vol"])[[2]]) > > However, the code below fails: > > #df.opt$Delta <- apply(df.opt, 1, function(x) > EuropeanOption(x$CallPutString, #x["UnderlyingPrice"], > x["StrikePrice"], 0, interest.rate, x["FractionalExpiration"], > #x["Vol"])[[2]]) > > Here df.opt is a data frame and European Option is a function in the > package RQuantLib. > The only difference between the two pieces of code is that a column of > the data frame is accessed using "$" in the first case and brackets in > the second case. > > Sorry if this is a trivial question, but could somebody tell me the > reason for this behavior? > > FS > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >