Kyle Brandt
Kyle Brandt

Reputation: 28437

Differences between hash and lists in R

In R, I find lists to be useful structures (like dictionaries in Python). I stumbled upon the hash package which seems to provide very similar functionality.

Are there any practical differences between lists and hashes that make one more desirable than the other? (Other than lists are part of the base)

I hope this isn't too open ended, but not sure how to narrow the scope of this.

Upvotes: 5

Views: 597

Answers (1)

Fojtasek
Fojtasek

Reputation: 3591

from the hash documentation:

PASS-BY REFERENCE. Environments and hashes are special objects in R because only one copy exists globally. When provide as an argument to a function, no local copy is made and any changes to the hash in the functions are reflected globally.

PERFORMANCE. Hashes are based on environments and are designed to be exceedingly fast using the environments internal hash table. For small data structures, a list will out-perform a hash in nearly every case. For larger data structure, i.e. >100-1000 key value pair the performance of the hash becomes faster. Much beyond that the performance of the hash far outperforms native lists.

Upvotes: 5

Related Questions