[R] attribute and main value

Gerrit Draisma gdraisma at xs4all.nl
Mon Dec 29 17:17:33 CET 2014


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
>



More information about the R-help mailing list