在R中表示哈希表/字典/地图的自然方式 [英] Natural way to represent hash tables/dictionaries/maps in R

查看:158
本文介绍了在R中表示哈希表/字典/地图的自然方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在R中使用字典/地图数据结构,类似于Python的 dict 或C ++ STL的 std :: map std :: hash_map



我可以执行以下所有操作。使用和/或性能有差异吗?如果是,在R中有这样的数据结构的最佳方式是什么?

 > mm = c()
> mm [my key] = 10
> mm [[我的钥匙]]
[1] 10
> mm
我的钥匙
10

> mm = list()
> mm [my key] = 10
> mm [[我的钥匙]]
[1] 10
> mm
$`my key`
[1] 10

> mm = vector()
> mm [my key] = 10
> mm [[我的钥匙]]
[1] 10
> mm
我的钥匙
10


解决方案

最快的是一个环境,因为默认情况下它们是散列的。

  e<  -  new.env()
e $ my_key< - 10
ls(e)


I want to use a dictionary/map data structure in R, similar to Python's dict or C++ STL's std::map or std::hash_map.

I can do all of the following. Is there a difference in their use and/or performance? And if so, which is the best way to have such a data structure in R?

> mm = c()
> mm["my key"] = 10
> mm[["my key"]]
[1] 10
> mm
my key 
    10

> mm = list()
> mm["my key"] = 10
> mm[["my key"]]
[1] 10
> mm
$`my key`
[1] 10

> mm = vector()
> mm["my key"] = 10
> mm[["my key"]]
[1] 10
> mm
my key 
    10 

解决方案

The fastest will be an environment, since they're hashed by default.

e <- new.env()
e$my_key <- 10
ls(e)

这篇关于在R中表示哈希表/字典/地图的自然方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆