本文介绍了索引操作删除属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
显然,用索引属性的列表没有返回的属性列表。
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 3 $b [1] 7 attr(,"x") [1] 67 > l[c('a','b')] $a [1] 1 2 3 $b [1] 7属性都没有了。是否有可能索引列表,而preserving其属性?
Attributes are gone. Is it possible to index a list while preserving its attributes?
推荐答案下面就是这样的一个子集的功能。需要注意的是它不试图覆盖的名字'属性是非常重要的。
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更多推荐
索引操作删除属性
发布评论