Thanks Duncan.

But my question was how to extract
simply the function value from value,
without the gradient attribute?

I see that things like value<2 give the right answer.
I was curiosity. I found now that value[1]
gives strips the attributes from value:
------
> value
[1] 1
attr(,"gradient")
[1] 2
> value[1]
[1] 1
------
Is that the way?

Gerrit


On 12/29/2014 05:05 PM, Duncan Murdoch wrote:
On 29/12/2014 10:32 AM, Gerrit Draisma wrote:
Just a curiosity question:

In the documentation for the nlm procedure
a find this example of defining a function
with a gradient attribute:
-----------
       f <- function(x, a)
       {
           res <- sum((x-a)^2)
           attr(res, "gradient") <- 2*(x-a)
           res
       }
-----------
I get the gradient with
       attr(f(3,2),"gradient")
but how do I get the function value it self?

value <- f(3,2)
gradient <- attr(value, "gradient")

Duncan Murdoch


______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.

Reply via email to