开发者

Is there a way to use arbitrary type of value as key in environment or named list in R?

开发者 https://www.devze.com 2023-04-06 07:34 出处:网络
I\'ve been looking for a proper implementation of hash map in R, with functionalitie开发者_如何转开发s similar to the map type in Python.

I've been looking for a proper implementation of hash map in R, with functionalitie开发者_如何转开发s 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 !


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 :)

0

精彩评论

暂无评论...
验证码 换一张
取 消