[R] sapply on data frames - $X vs.["X"]

Fernando Saldanha fsaldan1 at gmail.com
Thu Jan 19 06:50:40 CET 2006


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




More information about the R-help mailing list