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]]

