indexing operation removes attributes

前端 未结 2 1580
盖世英雄少女心
盖世英雄少女心 2021-01-14 04:13

Apparently, indexing a list with attributes returns a list without the attributes.

> l <- list(a=1:3, b=7)
> attr(l, \'x\') <- 67
> l
$a
[1] 1         


        
相关标签:
2条回答
  • 2021-01-14 04:32

    Use the sticky package. It was designed exactly for this purpose. (Full Disclosure: I am the package author.) It's simple to use, just call sticky() on your vector/list/etc. For example:

    > l <- list(a=1:3, b=7)
    > attr(l, 'x') <- 67    
    > l <- sticky(l) 
    > attr(l,'x')  
    > [1] 67
    >
    > class(l)
    > [1] "sticky" "list" 
    
    0 讨论(0)
  • 2021-01-14 04:53

    Here is such a subset function. Note that it is important to not try to overwrite the 'names' attribute.

    subset.with.attributes <- function(X, ...) {
     l <- X[...]
     attr.names <- names(attributes(X))
     attr.names <- attr.names[attr.names != 'names']
     attributes(l)[attr.names] <- attributes(X)[attr.names]
     return(l)
    }
    
    > subset.with.attributes(l, c('a','b'))
    $a
    [1] 1 2 3
    
    $b
    [1] 7
    
    attr(,"x")
    [1] 67
    

    Trying to simply assign the attributes will result in the subset failing if it actually does any subsetting.

    > subset.with.attributes(l, c('b'))
    $b
    [1] 7
    
    attr(,"x")
    [1] 67
    
    0 讨论(0)
提交回复
热议问题