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
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.
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.
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.
The fastest will be an environment, since they're hashed by default.
e <- new.env()
e$my_key <- 10
ls(e)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With