Re: [R] hash or other quick lookup function?

From: Duncan Murdoch <murdoch_at_stats.uwo.ca>
Date: Tue, 27 May 2008 07:55:05 -0400

Duncan Murdoch wrote:
> Esmail Bonakdarian wrote:
>
>> Hello all,
>>
>> I have a matrix of bit values.
>>
>> I compute certain values based on the bits in each row. There may be
>> *duplicate* entries in the matrix, ie several rows may be identical.
>> These rows change over time, and identical entries may not be next to
>> each other.
>>
>> Computing these values is time consuming, so I would like a way to store a
>> value once it's computed. That way I would be able to look up the value
>> based on a given bitstring (row in the matrix) and avoid having to re-compute
>> the same value.
>>
>> So, my thought is a hash function based on a bit string - does R have such
>> a thing? Or is there an alternative approach that would do the same in R?
>> The lookup should be quick, otherwise the gains from avoiding
>> recomputing identical values would be lost to some extent.
>>
>> I wasn't able to find anything via searches, I am hoping someone can
>> point me in the right direction.
>>
>>
>
> Environments can be hashed. To use this, you'd convert the bit string
> into a character string, and use that as the name of a variable in an
> environment.
>
> For example,
>
> cache <- new.env(hash=TRUE, parent=emptyenv())
> ...
> bits <- '0101'
> if (exists(bits, cache)) {
> return(get(bits, cache))
> } else {
> value <- long.computation()
> assign(bits, value)
>

Whoops, that assignment should have been in the cache:

        assign(bits, value, envir=cache)
> return(value)
> }
>
>
> 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.
>



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 Tue 27 May 2008 - 12:36:29 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 Wed 28 May 2008 - 13:30:40 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