r - R Hash - 根据值获取密钥
问题描述
H = hash()
H[["numbers"]] = c(1,2,3)
H[["alpha"]] = c("x","y","z")
H[["animals"]] = c("cat","dog")
现在我想使用“猫”来获取“动物”。根据值获取哈希的键。
解决方案
我想你正在寻找hash::invert
.
invert(H)[["cat"]]
[1] "animals"
这也适用于具有相同值的多个哈希。
H[["mammals"]] = c("cat","dog","human")
invert(H)[["cat"]]
[1] "animals" "mammals"
话虽如此,我同意其他答案,即这是一种执行任务的低效方式。
推荐阅读
- javascript - express.js:返回异步 json
- javascript - 无法使用 API 读取未定义的属性“forEach”
- sql - 如何从 SNOWFLAKE 的 SQL 表中的 json 字段中选择某些键/值对
- node.js - 节点 js 中未定义 ERR
- jquery - 添加的表格行最终在表格上方而不是在表格中
- scala - 在Scala中使用函数样式查找与谓词匹配的所有两个集合对
- r - 重塑R中的多列
- kubernetes-helm - Helm 在 llops 中使用模板函数
- tinymce - 如何限制 tinyMCE 中的缩进
- spring-boot - SprintBoot kafka 值序列化器