[R] Which data structure in R can mimic hash in PERL best?

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Tue Nov 18 13:11:34 CET 2008


Leon Yee wrote:
> Hi,
>
> Hans W. Borchers wrote:
>>> Dear all,
>>>
>>>    Which data structure in R can mimic hash in PERL? I'd like to set
>>> up a lookup table, which could be accomplished by HASH if using PERL.
>>> Which of the data structures in R is the most efficient for lookup
>>> table?
>>> Thanks for your help.
>>
>> The regular answer to this is "named arrays/vectors" or
>> "environments" and has been given several times here on R-help.
>> Unfortunately, everybody is applying a different terminology such as
>> 'maps', 'hashes', 'tables' or 'dictionaries', etc., so it's difficult
>> to search for those entries.
>>
>
>   Thank you all for the help. Actually, I just used named vectors to
> solve my problem. I am not sure whether there is some difference in
> efficiency when comparing named vectors/arrays and environments.

there is. keys in named vectors are sought in O(n), and hashed
environments have a separate index structure supposed to approximate
O(1) retrieval:

keys = as.vector(outer(letters, letters, paste))
alpha = keys[1]
omega = keys[length(keys)]

hash = new.env(hash=TRUE, parent=emptyenv(), size=100L)
for (key in keys) assign(key, key, hash)

vect = keys
names(vect) = keys

n = 100000
system.time(replicate(n, vect[alpha]))
system.time(replicate(n, vect[omega]))

system.time(replicate(n, get(alpha, hash)))
system.time(replicate(n, get(omega, hash)))

vQ



More information about the R-help mailing list