索引操作删除属性

编程入门 行业动态 更新时间:2024-10-28 10:36:36
本文介绍了索引操作删除属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

显然,用索引属性的列表没有返回的属性列表。

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

更多推荐

索引操作删除属性

本文发布于:2023-10-13 17:05:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1488567.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:索引   属性   操作

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!