[R] names in R list's

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Mon Sep 7 15:25:45 CEST 2015

On Mon, Sep 7, 2015 at 11:34 AM, Witold E Wolski <wewolski at gmail.com> wrote:
> What is the access time for R lists given a name of list element, is it
> linear, log, or constant?

 Try it and see?

> Than what are to rules for names in R-lists
> That reusing names is possible makes me wonder.
> tmp <- as.list(c(1,2,3,4))
> names(tmp) = c("a","a","b","b")
> tmp
> tmp$a
> What I am looking for is a standard R data structure which will allow me
> for fast and name lookup.

 Depends what you mean by "standard"?

 There's a `hash` package that implements what are variously known as
hash tables or associative arrays (in perl) and dictionaries (in

 > require(hash)
 > z=hash("a",99)
 > z
<hash> containing 1 key-value pair(s).
  a : 99
 > z$b=123

There's no ordering so numeric indexing fails:

 > z[1]
Error in get(k, x) : invalid first argument
 > z[[1]]
Error in z[[1]] : wrong arguments for subsetting an environment
 > z
<hash> containing 2 key-value pair(s).
  a : 99
  b : 123

and the keys are unique, even if you try:

 > z=hash(c("a","b","b","c"), 1:4)
 > z
 <hash> containing 3 key-value pair(s).
  a : 1
  b : 3
  c : 4

I guess the lookup uses the usual fast hash lookup algorithms, but
you'd have to check the docs and source for details.


More information about the R-help mailing list