indexing operation removes attributes

前端 未结 2 1598
盖世英雄少女心
盖世英雄少女心 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" 
    

提交回复
热议问题