Does R have a β€œdict” like in python or a map like in C ++?

I'm new to R programming. After checking out some of the lessons, I took most of the things I needed, but another thing is still missing: a data structure map.

Does everyone know, does R know? In which can I store pairs (key, value)?

Thanks!!

+8
r
source share
5 answers

Yes, that's what list is called.

 > x <- list(a=1, b="foo", c=c(1,1,2,3,5)) > x $a [1] 1 $b [1] "foo" $c [1] 1 1 2 3 5 

In Python, it's called a dict , for what it's worth it.

+8
source share

Environments are also a candidate, and in many cases the best option.

 e<-new.env(hash=T) e$a<-1 e$b<-2 R> e$a [1] 1 

The disadvantage of the list is that it is a linear search.

+5
source share

There is a hash package.

+3
source share

Since array / vector elements can be named, you get some properties of the built-in maps / dictionaries.

 x <- c(apple = 1, banana = 99, "oranges and lemons" = 33) x["apple"] x[c("bananas", "oranges and lemons")] x[x == 99] 

(If your values ​​are of different types, then you need to use list instead of a vector.)

+2
source share

The hash package, as mentioned above, adds a bit of overhead, but provides flexible, intuitive methods for accessing the map / hash / dictionary. It should be very easy for users from another language to understand it.

A list is the best solution if the list contains a small number of elements. (<200 or so).

It’s best to use an environment if you absolutely cannot bear the small overhead and don’t need flexible, intuitive methods.

A hash packet is best in most situations.

FROM -

0
source share

All Articles