Before using R, I used quite a bit of Perl. In Perl, I would often use hashes, and lookups of hashes are generally regarded as fast in Perl.
For example, the followi
First off, as Vince and Dirk has said, you are not using hashes in your example code. A literal translation of the perl example would be
#!/usr/bin/Rscript
testHash <- new.env(hash = TRUE, size = 10000L)
for(i in 1:10000) {
key <- paste(sample(letters, 3, replace = TRUE), collapse = "")
assign(key, floor(1000*runif(1)), envir = testHash)
}
keyArray <- ls(envir = testHash)
keyLen <- length(keyArray)
for(j in 1:10000) {
key <- keyArray[sample(keyLen, 1)]
lookupValue <- get(key, envir = testHash)
cat(paste("key", key, "Lookup", lookupValue, "\n"))
}
which runs plenty fast on my machine, them main time being the setup. (Try it and post the timings.)
But the real problem, as John said, is that you have to think vectors in R (like map in perl) and his solution is probably the best. If you do want to use hashes, consider
keys <- sample(ls(envir = testHash), 10000, replace = TRUE)
vals <- mget(keys, envir = testHash)
after the same setup as above, which is near-instantaneous on my machine. To print them all try
cat(paste(keys, vals), sep="\n")
Hope this helps a little.
Allan