+3 votes
in Programming Languages by (34.9k points)
edited by
Is there dictionary functionality like Python in R? If yes, how can I define a dictionary in R?

1 Answer

+1 vote
by (157k points)
selected by
Best answer

There is an R package, 'Dict', which implements a key-value dictionary data structure. Using this package, you can define a dictionary and access its elements using keys. The definition of an R dictionary is not very much similar to a Python dictionary, but you can access dictionary elements using keys in a Pythonic way.

You need first to install the package using the following command:


Here is an example:


# Create a new dictionary

x2 <- Dict$new(a=100, b=200, c=300)

# access the elements of the dictionary

print(c(x2['a'], x2['b'], x2['c']))

The above code will print "100 200 300".