[R] attribute and main value

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


Thanks Bill,
That is what I was looking for.
Gerrit

On 12/29/2014 05:53 PM, William Dunlap wrote:
> as.vector(x) will return x without any attributes and
> structure(x, attrA=NULL, attrB=NULL) will return x
> without the named attributes.
>     > z <- f(1:3, 4)
>     > z
>     [1] 14
>     attr(,"gradient")
>     [1] -6 -4 -2
>     > as.vector(z)
>     [1] 14
>     > structure(z, gradient=NULL)
>     [1] 14
>
> as.vector is a generic function, so for certain classes
> (e.g., factor) it may do more than just strip attributes.
> Matrix dimensions are attributes and will be removed by
> as.vector.
>
>
>
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com <http://tibco.com>
>
> On Mon, Dec 29, 2014 at 8:17 AM, Gerrit Draisma <gdraisma at xs4all.nl
> <mailto:gdraisma at xs4all.nl>> wrote:
>
>     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 at r-project.org <mailto:R-help at r-project.org> mailing list --
>     To UNSUBSCRIBE and more, see
>     https://stat.ethz.ch/mailman/__listinfo/r-help
>     <https://stat.ethz.ch/mailman/listinfo/r-help>
>     PLEASE do read the posting guide
>     http://www.R-project.org/__posting-guide.html
>     <http://www.R-project.org/posting-guide.html>
>     and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list