[R] how to do java-like hashtables in R, R from a java programmer's p onit of view

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Wed Apr 25 22:06:02 CEST 2001


Chris Marshall <chrism at norcomnetworks.com> writes:

> In R, you might proceed as follows to build such a table.  Say x is some
> list object.  Say you have a loop that is reading key/value pairs into the
> variables "key" and "value" one after another.
> 
> Putting the pair in the table is as simple as:
> 
> x[[key]] <- value
> 
> Later, retrieving a value from a key would be:
> 
> x[[key]]

Actually, the linear structure of a list in R makes this rather
inefficient. Another option is to use environments and pretend that
the key is a variable name:

e <- new.env()
assign(key,value,envir=e)
get(key,envir=e)

This has the advantage that environments, contrary to lists, really
are hashed. Take a peek inside envir.c if you care.

(Abandon all hopes of S-PLUS compatibility if you do this, though.
Also beware that environments are slightly strange objects since they
are never duplicated - e.g. on assignment.)

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list