2

I've been looking for a proper implementation of hash map in R, with functionalities similar to the map type in Python.

After some googling and searching the R documentations, I found that environment and named list are the ONLY options I can use (is that really so?).

But the problem with the two is that they can only take charaters as key for the hashing, not even a number, let alone other type of things.

So is there a way to use arbitrary things as key? or at least more than just characters.

Or is there a better implemtation of hash map that I didn't find with better functionalities ?

Thanks in advance.

Edit:

My current problem: I need a map to store the distance relationship between data points. That is, the key of the map is a tuple (p1, p2) and the value is a number.

The reason I asked a generic question instead of a concrete one is that I'm learning R recently and I want to know how to manipulate some of the most fundamental data structures, not only what my problem refers to. So I may need to use other things as key in the future, and I want to avoid asking similar questions with only minor difference every time I run into them.

Edit 2:

I got a lot of very good advices on this topic. It seems I'm still thinking quite in the Pythonic way, rather than the should-be R way. I should really get more R-ly ! I think my purpose can easily be satisfied by a matrix in R. Thanks All !

Derrick Zhang
  • 21,201
  • 18
  • 53
  • 73
  • 1
    As in the comments to your earlier (very similar) question http://stackoverflow.com/q/7432248/602276, can you please give an example of what you are trying to do? It will be easier to give advice on a specific issue rather than a generic question. It may be that `data.table` does what you want, if used correctly, but I won't know for certain until I understand your use case. – Andrie Sep 19 '11 at 08:56
  • For your current problem, why not a matrix? See the `dist` function for the most common built-in methods. – Aaron left Stack Overflow Sep 19 '11 at 17:22
  • @Aaron , I don't use matrix because the point is a vector of tens of numbers, can matrix represent this form of data efficiently and without too many code ? – Derrick Zhang Sep 20 '11 at 03:48
  • @SpiritZhang : a dist object is the most convenient way of representing this data. This converts to and from a matrix very easily. – Joris Meys Sep 20 '11 at 17:06
  • Example application: for each row in a data frame, add a column that is a mapped value. For example map "file suffix" (.exe, .dmg, ..) => "file type" (software, content, ..). – dfrankow May 24 '12 at 22:48

1 Answers1

2

The reason people keep asking you for a specific example is that most problems for which hash tables are the appropriate technique in Python have a good solution in R that does not involve hash tables.

That said, there are certainly times when a real hash table is useful in R, and I recommend you check out the hash package for R. It uses environments as its base but lets you do a lot of R-like vector work with them. It's efficient and I've never run into a problem with it.

Just keep in mind that if you're using hash tables a lot while working with R and your code is running slowly or is buggy, you may be able to get some mileage from figuring out a more R-like way of doing it :)

Peter M
  • 844
  • 5
  • 14