Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Natural way to represent hash tables/dictionaries/maps in R

Tags:

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 
like image 964
highBandWidth Avatar asked Nov 28 '11 16:11

highBandWidth


People also ask

Do dictionaries have maps?

A dictionary doesn't have positions, but rather maps one object to another; here it maps a grocery item to its quantity. You can think of a list as a structure that maps an integer (index 0, 1, 2, 3 ...) to an object; a list can be indexed only by an integer.

Why dictionaries are considered as associative arrays or mappings or hashes?

Dictionaries are often also called maps, hashmaps, lookup tables, or associative arrays. They allow the efficient lookup, insertion, and deletion of any object associated with a given key.

Does R have hash tables?

UPDATE: It turns out, R has a perfectly performant hash table implementation, it's just not intuitively named or easy to find. If you create a new environment using new. env(hash=TRUE) , R provides you an environment that performs admirably.


1 Answers

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

e <- new.env()
e$my_key <- 10
ls(e)
like image 52
Joshua Ulrich Avatar answered Feb 08 '23 03:02

Joshua Ulrich