Reputation: 17306
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
Upvotes: 22
Views: 16169
Reputation: 176698
The fastest will be an environment, since they're hashed by default.
e <- new.env()
e$my_key <- 10
ls(e)
Upvotes: 23