[R] Extracting formulae from expression() / deriv()

nickymcp nickymcpherson at gmail.com
Tue Apr 6 14:09:33 CEST 2010


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.



More information about the R-help mailing list