[R] Is there a hash data structure for R

Martin Møller Skarbiniks Pedersen tr@xp|@yer @end|ng |rom gm@||@com
Tue Nov 2 15:42:34 CET 2021


On Tue, 2 Nov 2021 at 10:48, Yonghua Peng <yong using pobox.com> wrote:
>
> I know this is a newbie question. But how do I implement the hash
structure
> which is available in other languages (in python it's dict)?
>

As other posters wrote then environments are the solution.
data.frames, vectors and lists are much slower and less useful to use as
key-value pairs.

Here are some code I somethings uses:

cache <- NULL

cache_set <- function(key, value) {
  assign(key, value, envir = cache)
}

cache_reset <- function() {
  cache <<- new.env(TRUE, emptyenv())
}

cache_get <- function(key) {
  get(key, envir = cache, inherits = FALSE)
}

cache_has_key <- function(key) {
  exists(key, envir = cache, inherits = FALSE)
}
cache_reset()

	[[alternative HTML version deleted]]



More information about the R-help mailing list