Re: [R] beginner Q: hashtable or dictionary?

From: Duncan Murdoch <murdoch_at_stats.uwo.ca>
Date: Tue 31 Jan 2006 - 02:18:24 EST

On 1/30/2006 9:55 AM, Seth Falcon wrote:

>> On 1/29/06, context grey <mobygeek@yahoo.com> wrote:
>>>
>>> Hi,
>>>
>>> Is there something like a hashtable or (python)
>>> dictionary in R/Splus?

>
> On 29 Jan 2006, jholtman@gmail.com wrote:
>> use a 'list':

>
> Most of the time, a list will be what you want, but it has some
> important differences from a Python dictionary. In particular, one
> can end up with duplicate keys. Here is an example:
>
> h <- list()
> h[["foo"]] <- 1
> h[[2]] <- 2
> names(h) <- rep("foo", 2)
> h
> $foo
> [1] 1
>
> $foo
> [1] 2
>
> h[["foo"]]
> [1] 1
>
> 'environments' may be what you are looking for, see help for new.env().
>
> h <- new.env(hash=TRUE)
>
> An important thing to keep in mind with environments, however, is that
> they are an exception to the pass by value semantics of the language.
> Environments are not copied when passed as function args. This has
> its uses, but can be confusing.

Both lists and environments have other oddities, too. For example, "mean" would be found in your environment above by functions like get(), since the base environment is its parent; "f" would be found in your list, since partial name matching is used in lists. You can avoid the parent problem in R 2.2.x+ by setting the parent explicitly to emptyenv().

Duncan Murdoch



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Tue Jan 31 02:32:42 2006

This archive was generated by hypermail 2.1.8 : Tue 31 Jan 2006 - 08:22:10 EST