I am attempting to extract the derivative/ gradient from this expression df1p <- deriv(f1, "P")> df1pexpression({ .value <- s - c - a * P .grad <- array(0, c(length(.value), 1L), list(NULL, c("P"))) .grad[, "P"] <- -a attr(.value, "gradient") <- .grad .value }) So in this case I want to extract the "-a". I have read the expression help file, which tells me that I should use [ and [[ to extract information, but the best result I've had so far is> df1p[attr(f1,"gradient")]expression() I'm sure I'm missing something really basic, but I've now lost several hours trying to do this and I'm getting pretty desperate. -- View this message in context: http://n4.nabble.com/Extracting-formulae-from-expression-deriv-tp1752738p1752738.html Sent from the R help mailing list archive at Nabble.com.
Henrique Dallazuanna
2010-Apr-06 13:05 UTC
[R] Extracting formulae from expression() / deriv()
Try this: attributes(eval(df1p))$gradient[,'P'] On Tue, Apr 6, 2010 at 9:09 AM, nickymcp <nickymcpherson at gmail.com> wrote:> > I am attempting to extract the derivative/ gradient from this expression > > df1p <- deriv(f1, "P") >> df1p > expression({ > ? ?.value <- s - c - a * P > ? ?.grad <- array(0, c(length(.value), 1L), list(NULL, c("P"))) > ? ?.grad[, "P"] <- -a > ? ?attr(.value, "gradient") <- .grad > ? ?.value > }) > > So in this case I want to extract the "-a". > > I have read the expression help file, which tells me that I should use [ and > [[ to extract information, but the best result I've had so far is > >> df1p[attr(f1,"gradient")] > expression() > > I'm sure I'm missing something really basic, but I've now lost several hours > trying to do this and I'm getting pretty desperate. > -- > View this message in context: http://n4.nabble.com/Extracting-formulae-from-expression-deriv-tp1752738p1752738.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
On Tue, Apr 6, 2010 at 9:09 AM, nickymcp <nickymcpherson at gmail.com <https://stat.ethz.ch/mailman/listinfo/r-help>> wrote:>**>* I am attempting to extract the derivative/ gradient from this expression *>* *>* df1p <- deriv(f1, "P") *>>* df1p *>* expression({ *>* .value <- s - c - a * P *>* .grad <- array(0, c(length(.value), 1L), list(NULL, c("P"))) *>* .grad[, "P"] <- -a *>* attr(.value, "gradient") <- .grad *>* .value *>* }) *>* *>* So in this case I want to extract the "-a". *>* *>* I have read the expression help file, which tells me that I should use [ and *>* [[ to extract information, but the best result I've had so far is *>* *>>* df1p[attr(f1,"gradient")] *>* expression() *>* *>* I'm sure I'm missing something really basic, but I've now lost several hours *>* trying to do this and I'm getting pretty desperate. Try this **ugly* but *straightforward approach:> x = df1p[[1]][[4]][[3]] > x-a> eval(x,list(a=1))-1> eval(x,list(a=5))[1] -5 Best, Dannemora * [[alternative HTML version deleted]]