首页 > 解决方案 > 尽管谓词为真,但对命名向量的名称使用过滤器会返回一个空的命名向量?

问题描述

考虑out<-setNames(sample(26,replace = T),letters)。通过构造,输出的元素之一必须命名为a。但是,当我尝试时Filter(function(x) names(x)=="a",out),我得到一个大小为 0 的命名数字。为什么会这样?

标签: rnames

解决方案


The function Filter loses the naming information of the vector (because it use lapply internally on out which also loses this information).

However, you can filter the names using: out[names(out) == "a"]. The result is a vector containing 6 with the name a.


推荐阅读