Re: [R] Hash table...

From: Duncan Murdoch <murdoch.duncan_at_gmail.com>
Date: Thu, 14 Apr 2011 06:46:33 -0400

On 11-04-14 4:48 AM, Philipp Pagel wrote:
> On Thu, Apr 14, 2011 at 06:44:53PM +1200, Worik R wrote:
>> To improve the efficiency of a process I am writing I would like to cache
>> results. So I would like a data structure like a hash table.
>>
>> So if I call Z<- f(Y) I can cache Z associated with Y: CACHE[Y]<- Z
>>
>> I am stumped. I expected to be able to use a list for this but I cannot
>> figure how....
>
> If y is an integer, factor or string you could try something along these
> lines:
>
> cache<- list()
> y<- 12
> cache[[as.character(y)]]<- sqrt(y)
> y<-98
> cache[[as.character(y)]]<- sqrt(y)
> cache
>
> $`12`
> [1] 3.464102
>
> $`98`
> [1] 9.899495
>
> Of course this can get you in trouble if y is a floating point
> number because of the issues with "identity" of such numbers, as
> discussed in ?all.equal and FAQ 7.31 "Why doesn't R think these
> numbers are equal?".
>

I haven't actually done timing, but if there are likely to be a lot of y values, I'd expect an environment created with hash=TRUE to be faster, both in adding new items and in retrieving existing ones. The code is pretty similar:

Use

cache <- new.env(hash=TRUE)

to create it, and

ls(cache)

to list the names, or

as.list(cache)

to print it as a list. Other than that, the assignment and retrieval code is identical.

Duncan Murdoch



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Thu 14 Apr 2011 - 10:52:10 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 14 Apr 2011 - 11:30:30 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive