Is there a hash structure in R?

11

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

This makes programming a lot easier.

    
asked by anonymous 29.04.2015 / 22:30

2 answers

4

As explained in @GuilhermeDuarte's own response, there is no essentially native hash structure of R, but there is a package that implements it.

It is important to note, however, that this package is useful only 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 excellent performance. However, the manual reports that the package only outperforms the base for hashes with more than 500 elements.

If the goal is to simplify code and organize variables by names, there are basically two ways to do this in R using basic structures, which have 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 named vectors. 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 names in a row, 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, lists can be used. The lists have several elaborate properties that fall outside 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 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 

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

    
30.04.2015 / 15:45
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 remove the values:

h[["carro"]]
    
29.04.2015 / 22:34