Is there a hash structure in R?

Asked

Viewed 274 times

12

Is there any hash structure in R, similar to Python and javascript Dictionaries?

This makes programming much easier.

  • 1

    If your goal is just the "idea" of hashes, in the key/value sense, you can simply use vectors with name, for example setNames(1:10, letters[1:10]), or lists, for data of different or more complex classes. The great advantage is working with basic structures compatible with virtually anything in R. The package hash will only be useful if you need to access a value by key and the hash size is > 500 (according to the package manual itself).

  • @Molx, very good comment! That’s what I wanted. You could put as an answer.

  • 1

    Environments can also be used as hash

2 answers

5

There may be a native solution, but an interesting solution is to use the "hash package".

For example,

library(hash)

Using the hash() function to create the hash:

h <- hash(c("carro", "caminhao", "trator"), c("ferrari", "benz", "deere")

And to withdraw the values:

h[["carro"]]

4


As explained in @Guilhermeduarte’s own reply, there is no essentially hash structure native to R, but there is one package that implements it.

It is important to note, however, that this package is only useful if it is necessary to take advantage of the great advantage of hashes, which is the possibility of obtaining a value from a key with great performance. Yet, the very manual informs that the package only exceeds the functions of the groundwork for hashes with more than 500 elements.

If the goal is to simplify code and organize variables by name, there are basically two ways to do this in R using the basic structures, which has the great advantage of direct compatibility with other native functions and third-party packages.

For simpler data, such as a sequence of elements of the same class (number, string, factor), called vectors, in R, we can use vectors with names. For example:

> vet <- 1:10
> vet
[1] 1 2 3 4 5 6 7 8 9 10

> names(vet) <- letters[1:10]
> vet
a  b  c  d  e  f  g  h  i  j
1  2  3  4  5  6  7  8  9 10

Each vector value can be accessed by its name using [[ ]], for example:

> vet[["d"]]
[2] 4

To create the vectors already with the names in a line, you can use setNames:

> vet2 <- setNames(11:20, letters[11:20])
> vet2
 k  l  m  n  o  p  q  r  s  t 
11 12 13 14 15 16 17 18 19 20 

For more complex data, you can use lists. The lists have several elaborate properties that are beyond the scope of this answer, but in terms of names, we can use:

> lista <- list(elemento1 = vet, elemento2=vet2)
> lista
$elemento1
 a  b  c  d  e  f  g  h  i  j  #Note que o nome dos elementos de vet foram mantidos, temos um vetor nomeado dentro de uma lista
 1  2  3  4  5  6  7  8  9 10 

$elemento2
 k  l  m  n  o  p  q  r  s  t 
11 12 13 14 15 16 17 18 19 20

To access the list element by name, we can do as in vectors:

> lista[["elemento1"]]
 a  b  c  d  e  f  g  h  i  j 
 1  2  3  4  5  6  7  8  9 10 

Or, using a "shortcut" that also makes the code more beautiful:

> lista$elemento1
 a  b  c  d  e  f  g  h  i  j 
 1  2  3  4  5  6  7  8  9 10 

Thus, I emphasize that if the goal is solely the organization of the code with a key/value structure, the native properties of R do this naturally. Details on the reasons why the package has superior performance for over 500 elements, as well as some information on Enviroments, which can also be used as hashes (as recalled by @Carloscinelli), can be found in the replies of that question Stack Overflow in English.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.