Equivalent python dict to R

I want to make the equivalent of a python dict in R. Basically, in python, I have:

visited = {} if atom_count not in visited: Do stuff visited[atom_count] = 1 

The idea is that if I saw this particular atom_count, I visited [atom_count] = 1. Thus, if I see this atom_count again, I’m not doing the stuff. Atom_Count is an integer.

Thank!

+44
dictionary r
May 21 '12 at 2:22
source share
2 answers

The closest thing to python dict in R is just a list. Like most R data types, lists can have a name attribute that allows lists to act as a set of name-value pairs:

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

Now for the usual disclaimer: they are not exactly the same; there will be differences. This way you will be disappointed trying to literally use lists exactly the way you can use dict in python.

+43
May 21 '12 at 4:21
source share

I believe that using a hash table (creating a new environment) may be the solution to your problem. I would say how to do it, but I just did it yesterday at talkstats.com.

If your dictionary is large and only two columns, then this may be the way to go. Here is a link to talkstats stream with sample R-code:

HASH TABLE LINK

+5
May 21 '12 at 5:45 am
source share



All Articles