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 ______________________________________________ R-help@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.