[R] beginner Q: hashtable or dictionary?
Duncan Murdoch
murdoch at stats.uwo.ca
Mon Jan 30 16:18:24 CET 2006
On 1/30/2006 9:55 AM, Seth Falcon wrote:
>> On 1/29/06, context grey <mobygeek at yahoo.com> wrote:
>>>
>>> Hi,
>>>
>>> Is there something like a hashtable or (python)
>>> dictionary in R/Splus?
>
> On 29 Jan 2006, jholtman at 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
More information about the R-help
mailing list