<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/populating-data-frame-cells-with-more-than-one-value/"></div></div><div><a data-url="http://www.r-bloggers.com/populating-data-frame-cells-with-more-than-one-value/" data-counturl="http://www.r-bloggers.com/populating-data-frame-cells-with-more-than-one-value/" data-count="horizontal" data-text="Populating data frame cells with more than one value" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://ryouready.wordpress.com/2016/07/18/populating-data-frame-cells-with-more-than-one-value/"> &quot;R&quot; you ready?</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<h3>Data frames are lists</h3>
<p>Most R users will know that data frames are lists. You can easily verify that a data frame is a list by typing</p>
<pre>
d &lt;- data.frame(id=1:2, name=c(&quot;Jon&quot;, &quot;Mark&quot;))
d
</pre>
<pre>
 id name
1 1 Jon
2 2 Mark
</pre>
<pre>
is.list(d)
</pre>
<pre>
[1] TRUE
</pre>
<p>However, data frames are lists with some special properties. For example, all entries in the list must have the same length (here 2), etc. You can find a nice description of the differences between lists and data frames <a target="_blank" href="http://stackoverflow.com/questions/15901224/what-is-difference-between-dataframe-and-list-in-r">here</a>. To access the first column of <tt>d</tt>, we find that it contains a vector (and a factor in case of column <tt>name</tt>). Note, that <tt>[[ ]]</tt> is an operator to select a list element. As data frames are lists, they will work here as well.<span></span></p>
<pre>
is.vector(d[[1]])
</pre>
<pre>
[1] TRUE
</pre>
<h3>Data frame columns can contain lists</h3>
<p>A long time, I was unaware of the fact, that data frames may also contain lists as columns instead of vectors. For example, let’s assume Jon’s children are <em>Mary</em> and <em>James</em>, and Mark’s children are called <em>Greta</em> and <em>Sally</em>. Their names are stored in a list with two elements. We can add them to the data frame like this:</p>
<pre>
d$children &lt;-  list(c(&quot;Mary&quot;, &quot;James&quot;), c(&quot;Greta&quot;, &quot;Sally&quot;))
d
</pre>
<pre>
 id name children
1 1 Jon Mary, James
2 2 Mark Greta, Sally
</pre>
<p>A single data frame entry in column <tt>children</tt> now contains more than one value. Given that the column is a list, not a vector, we cannot go as usual when modifying an entry of the column. For example, to change Jon’s children, we cannot do</p>
<pre>
> d[1 , &quot;children&quot;] &lt;- c(&quot;Mary&quot;, &quot;James&quot;, &quot;Thomas&quot;)

Error in `[&lt;-.data.frame`(`*tmp*`, 1, &quot;children&quot;, value = c(&quot;Mary&quot;, &quot;James&quot;, :
replacement has 3 rows, data has 1
</pre>
<p>Taking into account the list structure of the column, we can type the following to change the values in a single cell.</p>
<pre>
d[1 , &quot;children&quot;][[1]] &lt;- list(c(&quot;Mary&quot;, &quot;James&quot;, &quot;Thomas&quot;))

# or also

d$children[1] &lt;- list(c(&quot;Mary&quot;, &quot;James&quot;, &quot;Thomas&quot;))
d
</pre>
<pre>
 id name children
1 1 Jon Mary, James, Thomas
2 2 Mark Greta, Sally
</pre>
<p>You can also create a data frame having a list as a column using the &lt;tt>data.frame&lt;/tt> function, but with a little tweak. The list column has to be wrapped inside the function &lt;tt>I&lt;/tt>. This will protect it from several conversions taking place in &lt;tt>data.frame&lt;/tt> (see &lt;tt>?I&lt;/tt> documentation).</p>
<pre>
d &lt;- data.frame(id = 1:2,
                   name = c(&quot;Jon&quot;, &quot;Mark&quot;),
                   children = I(list(c(&quot;Mary&quot;, &quot;James&quot;),
                                     c(&quot;Greta&quot;, &quot;Sally&quot;)))
                )
</pre>
<p>This is an interesting feature, which gives me a deeper understanding of what a data frame is. But when exactly would I want to use it? I have not encountered the need to use it very often yet (though of course there may be plenty of situations where it makes sense). But today I had a case where this feature seemed particularly useful.</p>
<h3>Converting lists and data frames to JSON</h3>
<p>I had two separate types of information. One stored in a data frame and the other one in a list Referring to the example above, I had</p>
<pre>
d &lt;- data.frame(id=1:2, name=c(&quot;Jon&quot;, &quot;Mark&quot;))
d
</pre>
<pre>
 id name
1 1 Jon
2 2 Mark
</pre>
<p>and</p>
<pre>
ch &lt;- list(c(&quot;Mary&quot;, &quot;James&quot;), c(&quot;Greta&quot;, &quot;Sally&quot;))
ch
</pre>
<pre>
[[1]]
[1] &quot;Mary&quot; &quot;James&quot;

[[2]]
[1] &quot;Greta&quot; &quot;Sally&quot;
</pre>
<p>I needed to return an array of JSON objects which look like this.</p>
<pre>
[
  {
    &quot;id&quot;: 1,
    &quot;name&quot;: &quot;Jon&quot;,
    &quot;children&quot;: [&quot;Mary&quot;, &quot;James&quot;]
  }, 
  {
    &quot;id&quot;: 2,
    &quot;name&quot;: &quot;Mark&quot;,
    &quot;children&quot;: [&quot;Greta&quot;, &quot;Sally&quot;]
  }
]
</pre>
<p>Working with the superb <em>jsonlite</em> package to convert R to JSON, I could do the following to get the result above.</p>
<pre>
library(jsonlite)

l &lt;- split(d, seq(nrow(d))) # convert data frame rows to list
l &lt;- unname(l)              # remove list names
for (i in seq_along(l))     # add element from ch to list
    l[[i]] &lt;- c(l[[i]], children=ch[i])

toJSON(l, pretty=T, auto_unbox = T) # convert to JSON
</pre>
<p>The results are correct, but getting there involved quite a number of tedious steps. These can be avoided by directly placing the list into a column of the data frame. Then <em>jsonlite::toJSON</em> takes care of the rest.</p>
<pre>
d$children &lt;- list(c(&quot;Mary&quot;, &quot;James&quot;), c(&quot;Greta&quot;, &quot;Sally&quot;))
toJSON(d, pretty=T, auto_unbox = T)
</pre>
<pre>
[
  {
    &quot;id&quot;: 1,
    &quot;name&quot;: &quot;Jon&quot;,
    &quot;children&quot;: [&quot;Mary&quot;, &quot;James&quot;]
  },
  {
    &quot;id&quot;: 2,
    &quot;name&quot;: &quot;Mark&quot;,
    &quot;children&quot;: [&quot;Greta&quot;, &quot;Sally&quot;]
  }
]
</pre>
<p>Nice <img alt="🙂" class="wp-smiley" src="https://s.w.org/images/core/emoji/72x72/1f642.png"> What we do here, is basically creating the same nested list structure as above, only now it is disguised as a data frame. However, this approach is much more convenient.</p>
<p> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ryouready.wordpress.com/1152/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/ryouready.wordpress.com/1152/" border="0"></a> <img alt="" src="http://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" border="0" data-recalc-dims="1"></p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/populating-data-frame-cells-with-more-than-one-value/"></div></div><div><a data-url="http://www.r-bloggers.com/populating-data-frame-cells-with-more-than-one-value/" data-counturl="http://www.r-bloggers.com/populating-data-frame-cells-with-more-than-one-value/" data-count="vertical" data-text="Populating data frame cells with more than one value" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://ryouready.wordpress.com/2016/07/18/populating-data-frame-cells-with-more-than-one-value/"> &quot;R&quot; you ready?</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/EodajU_n9lQ"></a></div>