Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why associative array of R is called list and not map/dictionary [closed]

I have recently started learning R language. I come from Java/Python background. One thing I found surprising is that associative array in R is called List and not something like Map or Dictionary.

I think that names like Map and Dictionary better convey the idea that data structure is associative array than list. Can you please let me know what is the background/reasoning behind this.

like image 875
MoveFast Avatar asked Jan 03 '13 06:01

MoveFast


2 Answers

S has a longer history than do either Java or Python. The terminology derives from LisP one of the first high level languages.

Furthermore, you can create what some people might call an associative array by using a named atomic vector:

 vec <- c(a=1,b=2,c=3)
 vec["b"]
 #b 
 #2 

There are two sort of vectors: "recursive" and "atomic". Lists are of the first sort. Both can be indexed by "name" if there are keys assigned to the elements. However the term "name" in R strictly refers to symbols that exist in an environment that have object values. See ?as.name. In R "names" or "symbols" are language objects that in code are not quoted, whereas character values are used to assign or extract value from data objects by keys.

like image 88
IRTFM Avatar answered Sep 20 '22 21:09

IRTFM


A "Map" is a chart of the world. To find things on a map you have to scan in two dimensions. This is O(n^2). A "Dictionary" is a book of words in alphabetical order. To find a word in a dictionary you have to do a binary search. This is O(log n) performance.

So neither of those words, to me, accurately portray the structure nor the performance of an associative array...

Mathematically speaking an associative array is simply a function over a discrete range. So they should just be called functions. Double-square brackets are just a syntactic annoyance. Why can't we do:

z = list()
z("foo") = c(1,2,3)
print(z("foo")) # prints 1 2 3
print(z("bar")) # errors
  • because R isn't like that.
like image 26
Spacedman Avatar answered Sep 21 '22 21:09

Spacedman