r - 尽管谓词为真,但对命名向量的名称使用过滤器会返回一个空的命名向量?
问题描述
考虑out<-setNames(sample(26,replace = T),letters)
。通过构造,输出的元素之一必须命名为a
。但是,当我尝试时Filter(function(x) names(x)=="a",out)
,我得到一个大小为 0 的命名数字。为什么会这样?
解决方案
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
.
推荐阅读
- xml - 是否可以使用 xslt 保留第一个打开标签和最后一个关闭标签?
- android - Kotlin 标准函数未解析参考 Android Studio
- android - 如何在 Flutter 中使用 ThemeData 在 Flutter 中手动设置暗模式?
- netbeans - 无法调用 java.sql.Connecton.preparestatement(String),因为 con 为空
- javascript - 将鼠标悬停在按钮上并更改其属性 html/js/css
- html - 页面边框结合引导容器
- angular - Angular - 对象可能是“未定义”
- javascript - 对象属性仅在函数内递增一次
- android - Android:“应用信息”内的应用图标不同
- list - 具有通用 Comparable 作为参数的 Dart List.sortedBy 函数不会操纵列表