<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/convert-data-frame-to-dictionary-list-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://statcompute.wordpress.com/2018/11/16/convert-data-frame-to-dictionary-list-in-r/"> S+/R – Yet Another Blog in Statistical Computing</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>In R, there are a couple ways to convert the column-oriented data frame to a row-oriented dictionary list or alike, e.g. a list of lists. </p>
<p>In the code snippet below, I would show each approach and how to extract keys and values from the dictionary. As shown in the benchmark, it appears that the generic R data structure is still the most efficient. </p>
<pre>
### LIST() FUNCTION IN BASE PACKAGE ###
x1 &lt;- as.list(iris[1, ])
names(x1)
# [1] &quot;Sepal.Length&quot; &quot;Sepal.Width&quot;  &quot;Petal.Length&quot; &quot;Petal.Width&quot;  &quot;Species&quot;
x1[[&quot;Sepal.Length&quot;]]
# [1] 5.1

### ENVIRONMENT-BASED SOLUTION ###
envn_dict &lt;- function(x) {
  e &lt;- new.env(hash = TRUE)
  for (name in names(x)) assign(name, x[, name], e)
  return(e)
}

x2 &lt;- envn_dict(iris[1, ])
ls(x2)
# [1] &quot;Petal.Length&quot; &quot;Petal.Width&quot;  &quot;Sepal.Length&quot; &quot;Sepal.Width&quot;  &quot;Species&quot;
x2[[&quot;Sepal.Length&quot;]]
# [1] 5.1

### COLLECTIONS PACKAGE ###
coll_dict &lt;-  function(x) {
  d &lt;- collections::Dict$new()
  for (name in names(x)) d$set(name, x[, name])
  return(d)
}

x3 &lt;- coll_dict(iris[1, ])
x3$keys()
# [1] &quot;Petal.Length&quot; &quot;Petal.Width&quot;  &quot;Sepal.Length&quot; &quot;Sepal.Width&quot;  &quot;Species&quot;
x3$get(&quot;Sepal.Length&quot;)
# [1] 5.1

### HASH PACKAGE ###
hash_dict &lt;- function(x) {
  d &lt;- hash::hash()
  for (name in names(x)) d[[name]] &lt;- x[, name]
  return(d)
}

x4 &lt;- hash_dict(iris[1, ])
hash::keys(x4)
# [1] &quot;Petal.Length&quot; &quot;Petal.Width&quot;  &quot;Sepal.Length&quot; &quot;Sepal.Width&quot;  &quot;Species&quot;
hash::values(x4, &quot;Sepal.Length&quot;)
# Sepal.Length
#          5.1

### DATASTRUCTURES PACKAGE ###
data_dict &lt;- function(x) {
  d &lt;- datastructures::hashmap()
  for (name in names(x)) d[name] &lt;- x[, name]
  return(d)
}

x5 &lt;- data_dict(iris[1, ])
datastructures::keys(x5)
# [1] &quot;Species&quot;      &quot;Sepal.Width&quot;  &quot;Petal.Length&quot; &quot;Sepal.Length&quot; &quot;Petal.Width&quot;
datastructures::get(x5, &quot;Sepal.Length&quot;)
# [1] 5.1

### FROM PYTHON ###
py2r_dict &lt;- function(x) {
  return(reticulate::py_dict(names(x), x, TRUE))
}

x6 &lt;- py2r_dict(iris[1, ])
x6$keys()
# [1] &quot;Petal.Length&quot; &quot;Sepal.Length&quot; &quot;Petal.Width&quot;  &quot;Sepal.Width&quot;  &quot;Species&quot;
x6[&quot;Sepal.Length&quot;]
# [1] 5.1

### CONVERT DATAFRAME TO DICTIONARY LIST ###
to_list &lt;- function(df, fn) {
  l &lt;- list()
  for (i in seq(nrow(df))) l[[i]] &lt;- fn(df[i, ])
  return(l)
}

rbenchmark::benchmark(replications = 100, order = &quot;elapsed&quot;, relative = &quot;elapsed&quot;,
                      columns = c(&quot;test&quot;, &quot;replications&quot;, &quot;elapsed&quot;, &quot;relative&quot;, &quot;user.self&quot;, &quot;sys.self&quot;),
  &quot;BASE::LIST&quot;              = to_list(iris, as.list),
  &quot;BASE::ENVIRONMENT&quot;       = to_list(iris, envn_dict),
  &quot;COLLECTIONS::DICT&quot;       = to_list(iris, coll_dict),
  &quot;HASH::HASH&quot;              = to_list(iris, hash_dict),
  &quot;DATASTRUCTURES::HASHMAP&quot; = to_list(iris, data_dict),
  &quot;RETICULATE::PY_DICT&quot;     = to_list(iris, py2r_dict)
)
#                     test replications elapsed relative user.self sys.self
#1              BASE::LIST          100   0.857    1.000     0.857    0.000
#2       BASE::ENVIRONMENT          100   1.607    1.875     1.607    0.000
#4              HASH::HASH          100   2.600    3.034     2.600    0.000
#3       COLLECTIONS::DICT          100   2.956    3.449     2.956    0.000
#5 DATASTRUCTURES::HASHMAP          100  16.070   18.751    16.071    0.000
#6     RETICULATE::PY_DICT          100  18.030   21.039    18.023    0.008
</pre>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://statcompute.wordpress.com/2018/11/16/convert-data-frame-to-dictionary-list-in-r/"> S+/R – Yet Another Blog in Statistical Computing</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/OrphPx6l2Yo" height="1"></a></div>