<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>Analytica Insights: R</title><description>Only the posts that use R, from the Analytica Insights archive.</description><link>https://analyticadss.com/insights/</link><language>en-us</language><atom:link href="https://analyticadss.com/insights/rss-r.xml" rel="self" type="application/rss+xml"/><item><title>Functional programming in R with purrr</title><link>https://analyticadss.com/insights/unleash-the-power-of-functional-programming-in-r-with-the-purrr-package/</link><guid isPermaLink="true">https://analyticadss.com/insights/unleash-the-power-of-functional-programming-in-r-with-the-purrr-package/</guid><description>A practical guide to purrr in R: map(), map_dbl() and pmap(), safely(), quietly() and possibly(), plus examples for CSVs, models per group and plots.</description><pubDate>Fri, 14 Apr 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;em&gt;Written some years ago. The ideas hold, but check package versions and API details against current documentation before relying on the code.&lt;/em&gt;&lt;/p&gt;&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;Most R code I inherit has the same shape: a &lt;code&gt;for&lt;/code&gt; loop, an accumulator initialised to &lt;code&gt;NULL&lt;/code&gt;, and an &lt;code&gt;if&lt;/code&gt; inside the loop that quietly does something different on the third iteration. It works. It’s also nearly impossible to test, because no piece of it is small enough to test on its own.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;purrr&lt;/code&gt; is the tidyverse’s answer to that. It gives you a family of functions that apply one function across a list and hand back something whose type you can predict, which sounds modest until you’ve spent an afternoon working out why &lt;code&gt;sapply()&lt;/code&gt; returned a matrix instead of a vector.&lt;/p&gt;
&lt;p&gt;This post covers the &lt;code&gt;map()&lt;/code&gt; family, the error wrappers I reach for most, and a few examples with real shapes: reading a directory of CSVs, fitting a model per group, generating a plot per subset. If you already write &lt;code&gt;lapply()&lt;/code&gt; everywhere, some of this will look like a rename. The parts that aren’t a rename are the ones worth your time.&lt;/p&gt;
&lt;hr/&gt;
&lt;h2 id=&quot;what-functional-programming-actually-buys-you&quot;&gt;What functional programming actually buys you&lt;/h2&gt;
&lt;p&gt;The one-line version: write functions that take inputs and return outputs, and don’t let them touch anything else.&lt;/p&gt;
&lt;p&gt;That constraint sounds academic. It isn’t. A function with no side effects returns the same answer every time you run it, so you can test it on its own, and the order you call things in stops mattering. Most of the debugging time I’ve lost in R has gone on the opposite: a variable modified three scopes away, or a loop whose result depended on what happened to be left in the environment from the previous run.&lt;/p&gt;
&lt;p&gt;Two things follow from it.&lt;/p&gt;
&lt;p&gt;Small functions are testable functions. Break a hundred-line pipeline into six named steps and you can check each against a handful of inputs. Leave it as one block and your only test is whether the whole thing crashed.&lt;/p&gt;
&lt;p&gt;Avoiding explicit loops also nudges you toward vectorised operations, which in R are the fast path. That’s a side benefit rather than the point, but it’s a real one.&lt;/p&gt;
&lt;hr/&gt;
&lt;h2 id=&quot;what-purrr-is&quot;&gt;What &lt;code&gt;purrr&lt;/code&gt; is&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;purrr&lt;/code&gt; is part of the tidyverse, and its job is applying functions over lists without the type surprises base R hands you.&lt;/p&gt;
&lt;p&gt;The naming is what I’d point at first. &lt;code&gt;map()&lt;/code&gt; returns a list. &lt;code&gt;map_dbl()&lt;/code&gt; returns a double vector. &lt;code&gt;map_chr()&lt;/code&gt; returns a character vector. That suffix is a contract: if your function returns something else, you get an error on that line rather than a confusing one four steps later. &lt;code&gt;sapply()&lt;/code&gt; simplifies to whatever it feels like, and I’ve been caught by that on data where the first group happened to have exactly one row.&lt;/p&gt;
&lt;p&gt;Beyond the naming, it handles nested structures without you flattening them by hand, it has real error handling (which gets its own section below), and it composes with &lt;code&gt;dplyr&lt;/code&gt; and &lt;code&gt;tidyr&lt;/code&gt; because it was built alongside them.&lt;/p&gt;
&lt;p&gt;It’s on CRAN.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;r&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;install.packages&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&amp;quot;purrr&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;library&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;purrr&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The rest of this post is the functions themselves, with examples you can paste.&lt;/p&gt;
&lt;hr/&gt;
&lt;h2 id=&quot;the-functions-worth-knowing&quot;&gt;The functions worth knowing&lt;/h2&gt;
&lt;p&gt;Four things cover most of what I use &lt;code&gt;purrr&lt;/code&gt; for. The &lt;code&gt;map()&lt;/code&gt; family, &lt;code&gt;pmap()&lt;/code&gt; for when several arguments vary together, the three error wrappers, and a couple of list utilities.&lt;/p&gt;
&lt;h2 id=&quot;a-the-map-family&quot;&gt;A. The map() family&lt;/h2&gt;
&lt;p&gt;This is the part you’ll use every day. &lt;code&gt;map()&lt;/code&gt; takes a list and a function, applies the function to each element, and returns a list the same length. The variants differ only in what they promise to return, and that promise is the whole value: &lt;code&gt;map_dbl()&lt;/code&gt; will error rather than quietly hand you a list.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;map()&lt;/code&gt;: Returns a list.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;map_lgl()&lt;/code&gt;: Returns a logical vector.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;map_int()&lt;/code&gt;: Returns an integer vector.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;map_dbl()&lt;/code&gt;: Returns a double vector.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;map_chr()&lt;/code&gt;: Returns a character vector.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;map_df()&lt;/code&gt;: Returns a data frame.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Example:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;r&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Define a list of numbers&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;number_list&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; list&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 3&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 4&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Square each number using map()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;squared_numbers&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; map&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;number_list&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ~&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; .x&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;^&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;squared_numbers&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;b-pmap&quot;&gt;B. pmap()&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;pmap()&lt;/code&gt; is for when more than one thing varies. Give it a list of argument lists and it walks them in parallel, which beats writing a loop with three indices you have to keep aligned by hand.&lt;/p&gt;
&lt;p&gt;Example:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;r&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Define two lists&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;list1&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; list&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;list2&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; list&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 5&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 6&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Add corresponding elements of the two lists using pmap()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;sum_list&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; pmap&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;list&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;list1&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; list2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ~&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; ..1&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; ..2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;sum_list&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;c-safely-quietly-and-possibly&quot;&gt;C. safely(), quietly(), and possibly()&lt;/h2&gt;
&lt;p&gt;Is this worth a whole section? If you have ever lost a two-hour batch job to one malformed file at element 400, yes. This is the part I’d read first if you’re processing files or hitting an API. In a loop, one bad element kills the whole run and you lose everything before it. These three wrappers turn a failure into a value you can inspect afterwards, so the run finishes and you find out what broke at the end rather than at element 400 of 2,000.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;safely()&lt;/code&gt;: Returns a list containing the result and any error encountered.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;quietly()&lt;/code&gt;: Returns a list containing the result, any warnings, and any messages.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;possibly()&lt;/code&gt;: Returns a default value if an error is encountered.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Example:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;r&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Define a list with numbers and a character&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;mixed_list&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; list&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &amp;quot;a&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Define a safely wrapped square function&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;safe_square&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; safely&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;~&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; .x&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;^&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Apply the safe_square function to the mixed_list&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;results&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; map&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;mixed_list&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; safe_square&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;results&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;d-compact-and-compose&quot;&gt;D. compact() and compose()&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;compact()&lt;/code&gt; is used to remove &lt;code&gt;NULL&lt;/code&gt; elements from a list, while &lt;code&gt;compose()&lt;/code&gt; allows you to combine multiple functions into a single function.&lt;/p&gt;
&lt;p&gt;Example:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;r&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Define a list with NULL elements&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;null_list&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; list&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; NULL&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; NULL&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Remove NULL elements using compact()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;clean_list&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; compact&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;null_list&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;clean_list&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Compose two functions: square and increment&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;square&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; function&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;x&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;x&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;^&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;increment&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; function&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;x&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;x&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;square_and_increment&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; compose&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;increment&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; square&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Apply the composed function to a number&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;result&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; square_and_increment&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;result&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That’s the core. What follows is the same functions on data with realistic shapes.&lt;/p&gt;
&lt;hr/&gt;
&lt;h2 id=&quot;practical-examples-with-purrr&quot;&gt;Practical examples with purrr&lt;/h2&gt;
&lt;p&gt;Four examples. Summary statistics across columns, a model per subgroup, a directory of CSVs read in one pass, and a plot per group.&lt;/p&gt;
&lt;h2 id=&quot;a-example-1-calculating-summary-statistics-for-multiple-variables&quot;&gt;A. Example 1: Calculating summary statistics for multiple variables&lt;/h2&gt;
&lt;p&gt;You have a data frame and you want the same set of statistics for every numeric column. The loop version needs a pre-allocated result and a counter. This version doesn’t.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;r&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Load required packages&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;library&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;dplyr&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;library&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;purrr&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Create a sample data frame&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;data&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; data.frame&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;  var1&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; rnorm&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; mean&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 10&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; sd&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;  var2&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; rnorm&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; mean&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 20&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; sd&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 5&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;  var3&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; rnorm&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; mean&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 30&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; sd&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;  stringsAsFactors&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; FALSE&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Define a list of summary functions&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;summary_functions&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; list&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;mean&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; mean&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; median&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; median&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; sd&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; sd&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Calculate summary statistics for each variable using nested map functions&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;summary_stats&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; map_dfr&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;summary_functions&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ~&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; map_dfc&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;data&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; .x&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; .id&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &amp;quot;Statistic&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;summary_stats&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;b-example-2-fitting-multiple-linear-models-for-different-subsets-of-data&quot;&gt;B. Example 2: Fitting multiple linear models for different subsets of data&lt;/h2&gt;
&lt;p&gt;Fitting one model per group is where &lt;code&gt;purrr&lt;/code&gt; starts paying for itself. Split, map, and you have a list of models you can then map over again to pull out coefficients or R-squared, without ever writing an index.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;r&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Load required packages&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;library&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;dplyr&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;library&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;purrr&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;library&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;broom&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Split the mtcars dataset by the number of cylinders&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;mtcars_split&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; mtcars&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; %&amp;gt;%&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; group_split&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;cyl&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Define a function to fit a linear model and extract coefficients&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;fit_lm&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; function&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;data&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;  model&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; lm&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;mpg&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ~&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; wt&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; data&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; data&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;  coef&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; data.frame&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;tidy&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;model&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;%&amp;gt;%&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;    select&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;term&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; estimate&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;%&amp;gt;%&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;    mutate&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;cyl&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; unique&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;data&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;$&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;cyl&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;coef&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Apply the fit_lm function to each subset using map_dfr()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;model_coefs&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; map_dfr&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;mtcars_split&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; fit_lm&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;model_coefs&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;c-reading-multiple-csv-files-with-purrr&quot;&gt;C. Reading Multiple CSV files with purrr&lt;/h2&gt;
&lt;p&gt;This is the case I use most often, and it’s also where &lt;code&gt;safely()&lt;/code&gt; earns its keep: one malformed file in a directory of two hundred should not cost you the other 199.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;r&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Define the directory containing the CSV files&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;csv_directory&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &amp;quot;path/to/your/csv/files&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# List all CSV files in the directory&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;csv_files&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; list.files&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;csv_directory&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; pattern&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &amp;quot;*.csv&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; full.names&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; TRUE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Define a function to read a CSV file and add a column with the filename&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;read_csv_with_filename&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; function&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;file&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;  data&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; read_csv&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;file&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;  data&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; data&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; %&amp;gt;%&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; mutate&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;filename&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; basename&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;file&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  return&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;data&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Read all CSV files using map_dfr() and bind the results into a single data frame&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;combined_data&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; map_dfr&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;csv_files&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; read_csv_with_filename&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We list the CSVs, define a reader, map it over the paths, then bind the results into one frame. Swap &lt;code&gt;map()&lt;/code&gt; for &lt;code&gt;map(safely(read_one))&lt;/code&gt; and a bad file becomes a row you can inspect rather than an error that ends the run.&lt;/p&gt;
&lt;h2 id=&quot;d-purrr-and-ggplot2&quot;&gt;D. purrr and ggplot2&lt;/h2&gt;
&lt;p&gt;Same idea, different output. Instead of a list of models, you get a list of ggplot objects, one per group, which you can then arrange on a grid.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;r&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Load required packages&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;library&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;purrr&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;library&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;ggplot2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;library&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;dplyr&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;library&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;cowplot&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Create a list of data frames, one for each unique number of cylinders in the mtcars dataset&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;data_list&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; mtcars&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; %&amp;gt;%&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;  split&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;$&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;cyl&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Define a function to create a ggplot for a given data frame&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;create_ggplot&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; function&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;data&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;  ggplot&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;data&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; aes&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;x&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; mpg&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; y&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; hp&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;    geom_point&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;aes&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;color&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; factor&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;gear&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; size&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;    labs&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;title&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; paste&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&amp;quot;Number of Cylinders:&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; unique&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;data&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;$&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;cyl&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;         x&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &amp;quot;Miles per Gallon&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;         y&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &amp;quot;Horsepower&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;    theme_minimal&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;    theme&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;legend.title&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; element_blank&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;()) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;    scale_color_discrete&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &amp;quot;Gears&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Create a list of ggplots using map()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;ggplot_list&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; data_list&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; %&amp;gt;%&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;  map&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;create_ggplot&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Combine the ggplots into a single plot using cowplot&amp;#39;s plot_grid()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;combined_plot&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; plot_grid&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;plotlist&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; ggplot_list&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; ncol&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; align&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &amp;quot;v&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; rel_heights&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; c&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Display the combined plot&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;print&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;combined_plot&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And this is what that one &lt;code&gt;map&lt;/code&gt; call produces — a plot per cylinder group, drawn by the same function:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://analyticadss.com/img/posts/purrr/mtcars-by-cylinder.webp&quot; alt=&quot;Three scatterplots of horsepower against miles per gallon, one panel per cylinder count, coloured by gear count — each panel generated by the same mapped function.&quot; width=&quot;720&quot; height=&quot;663&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;/&gt;&lt;/p&gt;
&lt;p&gt;In this example, we first create a list of data frames, one for each unique number of cylinders in the &lt;code&gt;mtcars&lt;/code&gt; dataset. Then, we define a custom function &lt;code&gt;create_ggplot()&lt;/code&gt; to create a ggplot for a given data frame. The function creates a scatterplot of miles per gallon (mpg) versus horsepower (hp), with a title that reflects the number of cylinders.&lt;/p&gt;
&lt;p&gt;Finally, we use &lt;code&gt;purrr&lt;/code&gt;‘s &lt;code&gt;map()&lt;/code&gt; function to apply the custom function to each data frame in the list, resulting in a list of ggplots. We use a for loop to display each ggplot.&lt;/p&gt;
&lt;p&gt;The plot we get can be seen below:&lt;/p&gt;
&lt;p&gt;In this example, we’ve made some changes to the &lt;code&gt;create_ggplot()&lt;/code&gt; function to improve the aesthetics of the plots:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;We use &lt;code&gt;geom_point(aes(color = factor(gear)), size = 3)&lt;/code&gt; to color the points by the number of gears and increase their size.&lt;/li&gt;
&lt;li&gt;We apply &lt;code&gt;theme_minimal()&lt;/code&gt; to use a minimalistic theme for the plots.&lt;/li&gt;
&lt;li&gt;We remove the legend title using &lt;code&gt;theme(legend.title = element_blank())&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;We rename the color scale to “Gears” using &lt;code&gt;scale_color_discrete(name = &amp;quot;Gears&amp;quot;)&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Finally, we use the &lt;code&gt;plot_grid()&lt;/code&gt; function from the &lt;code&gt;cowplot&lt;/code&gt; package to combine the ggplots in the &lt;code&gt;ggplot_list&lt;/code&gt; into a single plot with one column and display the combined plot.&lt;/p&gt;
&lt;p&gt;The pattern in all four is the same: name the operation you want to do once, then map it. What changes is what comes back.&lt;/p&gt;
&lt;hr/&gt;
&lt;h2 id=&quot;tips-and-best-practices-for-using-purrr&quot;&gt;Tips and Best Practices for Using purrr&lt;/h2&gt;
&lt;p&gt;A few things I’d tell someone picking this up.&lt;/p&gt;
&lt;h2 id=&quot;1-use-anonymous-functions-when-appropriate&quot;&gt;1. Use anonymous functions when appropriate&lt;/h2&gt;
&lt;p&gt;When using &lt;code&gt;map()&lt;/code&gt; functions, you can create anonymous functions using the &lt;code&gt;~&lt;/code&gt; notation, which allows for concise and readable code. However, if the function becomes too complex or is used multiple times, consider defining it as a separate named function for better code organization and readability.&lt;/p&gt;
&lt;h2 id=&quot;2-compose-functions-instead-of-nesting-them&quot;&gt;2. Compose functions instead of nesting them&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;compose()&lt;/code&gt; builds a new function out of existing ones, right to left. Two small named functions and a &lt;code&gt;compose()&lt;/code&gt; beats one function doing both jobs, mostly because you can test each half.&lt;/p&gt;
&lt;h2 id=&quot;3-wrap-anything-that-touches-the-outside-world&quot;&gt;3. Wrap anything that touches the outside world&lt;/h2&gt;
&lt;p&gt;Files, APIs, database calls: use &lt;code&gt;safely()&lt;/code&gt;, &lt;code&gt;quietly()&lt;/code&gt; or &lt;code&gt;possibly()&lt;/code&gt;. A single bad element should cost you that element, not the run.&lt;/p&gt;
&lt;h2 id=&quot;4-know-when-to-use-purrr-vs-base-r-or-dplyr&quot;&gt;4. Know when to use purrr vs. base R or dplyr&lt;/h2&gt;
&lt;p&gt;While &lt;code&gt;purrr&lt;/code&gt; provides a powerful and flexible way to manipulate data, there are cases where base R or &lt;code&gt;dplyr&lt;/code&gt; functions may be more appropriate or efficient. For example, if you need to perform simple operations on a data frame, consider using &lt;code&gt;dplyr&lt;/code&gt; functions like &lt;code&gt;mutate()&lt;/code&gt; or &lt;code&gt;summarize()&lt;/code&gt;. Evaluate the needs of your specific task and choose the best tool for the job.&lt;/p&gt;
&lt;h2 id=&quot;5-read-the-reference-not-just-the-cheatsheet&quot;&gt;5. Read the reference, not just the cheatsheet&lt;/h2&gt;
&lt;p&gt;There’s more in the package than the &lt;code&gt;map()&lt;/code&gt; family, and the reference is short. &lt;code&gt;imap()&lt;/code&gt;, &lt;code&gt;walk()&lt;/code&gt; and &lt;code&gt;reduce()&lt;/code&gt; are the three I wish I’d found sooner.&lt;/p&gt;
&lt;p&gt;None of this is a rule. But if you write R for a living, the error wrappers alone will save you an afternoon within the month.&lt;/p&gt;
&lt;hr/&gt;
&lt;h2 id=&quot;wrapping-up&quot;&gt;Wrapping up&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;purrr&lt;/code&gt; is a rename of &lt;code&gt;lapply()&lt;/code&gt; right up until it isn’t. The &lt;code&gt;map_*()&lt;/code&gt; suffixes stop type surprises at the line that caused them, &lt;code&gt;pmap()&lt;/code&gt; removes the multi-index loop, and the error wrappers turn a crashed batch job into a result set with some failures in it.&lt;/p&gt;
&lt;p&gt;If you take one thing: wrap anything that touches a file or a network in &lt;code&gt;safely()&lt;/code&gt; before you run it over two thousand elements. I have learned that one the expensive way.&lt;/p&gt;
&lt;p&gt;Happy coding.&lt;/p&gt;
&lt;h2 id=&quot;further-reading&quot;&gt;Further reading&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;The &lt;a href=&quot;https://purrr.tidyverse.org/&quot; rel=&quot;noopener noreferrer&quot; target=&quot;_blank&quot;&gt;purrr reference&lt;/a&gt; — short, and the examples run.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;R for Data Science&lt;/em&gt;, Wickham and Grolemund. The iteration chapter is the gentlest introduction to this material anywhere.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Advanced R&lt;/em&gt;, Wickham. Read the Functionals chapter when you want to know why &lt;code&gt;map()&lt;/code&gt; is built the way it is.&lt;/li&gt;
&lt;li&gt;The Posit Community forum, if you are stuck on something specific.
Harnessing these resources and proactively mingling with the wider R circle will undoubtedly refine your prowess with both the &lt;code&gt;purrr&lt;/code&gt; package and R’s functional programming realm. Continue your journey of discovery, trial, and collaborative learning to blossom as an adept data scientist and R aficionado.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Artificial Intelligence Data Science functional programming Programming R Rstats&lt;/p&gt;</content:encoded><dc:creator>Dr. Aous Abdo</dc:creator><category>R</category><category>Functional programming</category><category>tidyverse</category></item><item><title>R on steroids: Rcpp</title><link>https://analyticadss.com/insights/r-on-steroids-with-rcpp-library/</link><guid isPermaLink="true">https://analyticadss.com/insights/r-on-steroids-with-rcpp-library/</guid><description>How to move one slow R loop into C++ with Rcpp&apos;s cppFunction(): a complete working example, a measured 24x speedup, and when it isn&apos;t worth it.</description><pubDate>Sun, 01 Jan 2023 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;em&gt;Written some years ago. The ideas hold, but check package versions and API details against current documentation before relying on the code.&lt;/em&gt;&lt;/p&gt;&lt;p&gt;Sometimes the honest answer to “why is my R slow?” is that the computation is a genuine loop, each step needing the last, and &lt;a href=&quot;https://analyticadss.com/insights/maximizing-efficiency-with-loops-and-vectorization-in-programming-languages/&quot;&gt;no amount of vectorizing&lt;/a&gt; will change that. For exactly those cases, R keeps an escape hatch with a beautiful power-to-effort ratio: Rcpp.&lt;/p&gt;
&lt;p&gt;The idea: write the one hot function in C++, and Rcpp compiles it and welds it into your session as if it were a normal R function. Everything else — data wrangling, plotting, the whole comfortable tidy world — stays in R. You’re not rewriting your project in C++. You’re extraditing one guilty function.&lt;/p&gt;
&lt;p&gt;Why does it work? R is interpreted; every pass through an R loop pays interpreter overhead per element. C++ is compiled to machine code before it runs, so a loop is just a loop. For scalar-heavy, iteration-heavy work the difference is routinely 10–100x.&lt;/p&gt;
&lt;p&gt;Here’s the entire workflow, using a cumulative-sum-with-a-twist that genuinely can’t be vectorized (each element depends on the previous &lt;em&gt;output&lt;/em&gt;):&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;r&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;install.packages&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&amp;quot;Rcpp&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;library&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;Rcpp&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;cppFunction&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&amp;#39;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;NumericVector decayCumsum(NumericVector x, double decay) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  int n = x.size();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  NumericVector out(n);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  out[0] = x[0];&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  for (int i = 1; i &amp;lt; n; i++) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;    out[i] = x[i] + decay * out[i - 1];&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;  return out;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;x&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; runif&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1e7&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;system.time&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;decayCumsum&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;x&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0.9&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))   &lt;/span&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# ~0.02s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# The same function in pure R&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;decayR&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; function&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;x&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; decay&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;  out&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; numeric&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;length&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;x&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)); &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;out&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; x&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;length&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;x&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)) &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;out&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; x&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; decay&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; out&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;  out&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;system.time&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;decayR&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;x&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0.9&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))        &lt;/span&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# ~0.46s on the same machine&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p tabindex=&quot;0&quot; role=&quot;region&quot; aria-label=&quot;Figure&quot;&gt;&lt;img src=&quot;https://analyticadss.com/img/posts/rcpp/rcpp-measured-speedup.svg&quot; alt=&quot;Two horizontal bars. The plain R loop over ten million elements takes 460 milliseconds; the identical loop written in C++ and compiled with Rcpp takes 19 milliseconds, a 24-fold speedup.&quot; width=&quot;760&quot; height=&quot;300&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;/&gt;&lt;/p&gt;
&lt;p&gt;Twenty-four times faster for fourteen lines of C++, and the calling code can’t tell the difference. &lt;code&gt;cppFunction&lt;/code&gt; handles the compiling, the type bridging and the loading, which are the parts that made “just write it in C” a week-long detour in the old days.&lt;/p&gt;
&lt;p&gt;That number used to be a hundred, and I’d have repeated it if I hadn’t re-run the benchmark for this figure. R grew a byte-code compiler that’s been on by default since version 3.4, and it’s very good at exactly this shape of loop. Turn it off with &lt;code&gt;compiler::enableJIT(0)&lt;/code&gt; and the R side goes back to 5.5 seconds, which is where the old hundred-fold figure came from. So the escape hatch is still worth having — it just has a smaller gap to jump than it used to, and the honest version of the claim is the one you measured this morning.&lt;/p&gt;
&lt;p&gt;When should you actually reach for this? My rule has three parts. Profile first, because the slow part is never where you think (&lt;code&gt;Rprof&lt;/code&gt; or the profvis package will tell you). Vectorize second, because if the loop &lt;em&gt;can&lt;/em&gt; become a vector operation, that’s free speed with no toolchain. Rcpp third, for the loops that survive: simulations, recursive filters, MCMC innards, tree traversals.&lt;/p&gt;
&lt;p&gt;And when shouldn’t you? When the bottleneck is I/O or memory rather than CPU, because C++ won’t make your disk faster. When the R version runs in eight seconds once a month. The maintenance cost of a second language in the project is real, and somebody who doesn’t know C++ will inherit this someday. Speed you don’t need is complexity you paid for anyway.&lt;/p&gt;
&lt;p&gt;But when the profile says one function is eating ninety percent of an hour-long run? Fourteen lines. It still feels like cheating.&lt;/p&gt;</content:encoded><dc:creator>Dr. Aous Abdo</dc:creator><category>R</category><category>C++</category><category>Performance</category></item><item><title>Loops vs vectorization: the 40x you&apos;re leaving on the table</title><link>https://analyticadss.com/insights/maximizing-efficiency-with-loops-and-vectorization-in-programming-languages/</link><guid isPermaLink="true">https://analyticadss.com/insights/maximizing-efficiency-with-loops-and-vectorization-in-programming-languages/</guid><description>The same computation, written two ways, with a benchmark that isn&apos;t close. Why vectorized code wins in R, and when a loop is still right.</description><pubDate>Thu, 01 Dec 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;em&gt;Written some years ago. The ideas hold, but check package versions and API details against current documentation before relying on the code.&lt;/em&gt;&lt;/p&gt;&lt;p&gt;Here’s a benchmark from my own machine: the same arithmetic on ten million numbers, once with a loop, once vectorized.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://analyticadss.com/img/posts/loops/loop-vs-vectorization-benchmark.webp&quot; alt=&quot;Loop time grows linearly to about 10 seconds at ten million calculations; the vectorized line stays flat near zero.&quot; width=&quot;743&quot; height=&quot;426&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;/&gt;&lt;/p&gt;
&lt;p&gt;The red line is the loop. The dashed line hugging the x-axis is vectorization doing the identical work. That’s not a tuning difference; that’s a factor of forty or more, and it’s sitting in most R scripts I get asked to look at.&lt;/p&gt;
&lt;p&gt;A loop says &lt;em&gt;do this to each element, one at a time&lt;/em&gt;. Vectorization says &lt;em&gt;do this to the whole vector&lt;/em&gt; and lets the language dispatch the iteration to compiled C underneath. Same maths, different addressee:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;r&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;x&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; runif&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1e7&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# The loop&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;out&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; numeric&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;length&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;x&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; seq_along&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;x&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;  out&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; x&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# The vectorized version&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;out&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; x&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Why is the gap so violent in R specifically? Because R is interpreted, and a loop pays interpreter overhead — type checks, dispatch, bookkeeping — on &lt;em&gt;every element&lt;/em&gt;. Ten million elements, ten million tolls. The vectorized call pays once, then runs machine code. Python has the same disease and the same cure, spelled NumPy.&lt;/p&gt;
&lt;p&gt;Two loop pitfalls do the most damage in practice. Growing an object inside a loop (&lt;code&gt;out &amp;lt;- c(out, ...)&lt;/code&gt;) reallocates the whole vector every pass and turns linear work quadratic. If you must loop, pre-allocate, as above. And the sneaky one: hiding loops inside loops, where an innocent &lt;code&gt;apply&lt;/code&gt; call inside a &lt;code&gt;for&lt;/code&gt; multiplies the toll.&lt;/p&gt;
&lt;p&gt;So should you vectorize everything? No, and knowing where the boundary sits is the actual skill. A loop is right when iterations genuinely depend on each other: a simulation where step &lt;em&gt;t&lt;/em&gt; needs step &lt;em&gt;t−1&lt;/em&gt;, like &lt;a href=&quot;https://analyticadss.com/insights/unsolved-math-problems-the-collatz-conjecture/&quot;&gt;the Collatz sequence&lt;/a&gt;, can’t be vectorized away. It’s right when each iteration is a heavy independent job (a model fit per file, where &lt;a href=&quot;https://analyticadss.com/insights/unleash-the-power-of-functional-programming-in-r-with-the-purrr-package/&quot;&gt;purrr’s map functions&lt;/a&gt; give you loop semantics with cleaner bones). And it’s right when clarity wins: a readable loop over a thirty-second job beats a clever one-liner nobody can maintain.&lt;/p&gt;
&lt;p&gt;But the default posture matters. In vector languages, reach for the vectorized form first and justify the loop, not the other way round. When you genuinely need loop logic at compiled speed, that escape hatch exists too — it’s called Rcpp, and &lt;a href=&quot;https://analyticadss.com/insights/r-on-steroids-with-rcpp-library/&quot;&gt;it gets its own post&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The benchmark above is one line of your time to reproduce. Run it once on your own machine. You’ll never write the naive loop again.&lt;/p&gt;</content:encoded><dc:creator>Dr. Aous Abdo</dc:creator><category>R</category><category>Python</category><category>Performance</category></item><item><title>The Collatz conjecture: two rules, no exit</title><link>https://analyticadss.com/insights/unsolved-math-problems-the-collatz-conjecture/</link><guid isPermaLink="true">https://analyticadss.com/insights/unsolved-math-problems-the-collatz-conjecture/</guid><description>Halve it if even, 3n + 1 if odd. Every number tested past 2⁶⁸ reaches 1, yet there&apos;s no proof. Why it&apos;s so hard, Tao&apos;s 2019 result, and R code to try it.</description><pubDate>Thu, 01 Dec 2022 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;em&gt;Written some years ago. The ideas hold, but check package versions and API details against current documentation before relying on the code.&lt;/em&gt;&lt;/p&gt;&lt;p&gt;Take any positive whole number. If it’s even, halve it. If it’s odd, multiply by three and add one. Repeat.&lt;/p&gt;
&lt;p&gt;Start with 5 and you get: 5 → 16 → 8 → 4 → 2 → 1. Start with 27 and you’re in for a ride — 111 steps, climbing as high as 9,232 before collapsing. But collapse it does, and that’s the conjecture Lothar Collatz posed in 1937: no matter where you start, you always reach 1.&lt;/p&gt;
&lt;p&gt;Always? Every number ever tested reaches 1, and the testing has gone past 2⁶⁸. No proof exists. Paul Erdős famously said mathematics isn’t ready for such problems, and offered $500 for a solution — pocket change against the &lt;a href=&quot;https://analyticadss.com/insights/riemann-hypothesis-a-mathematical-exploration/&quot;&gt;million on Riemann&lt;/a&gt;, yet the problem has eaten just as many careers.&lt;/p&gt;
&lt;h2 id=&quot;why-so-hard-when-the-rule-is-so-small&quot;&gt;Why so hard, when the rule is so small?&lt;/h2&gt;
&lt;p&gt;Because the sequence has no respect for size or pattern. Whether you rise or fall next depends only on parity, and parity after tripling-plus-one is chaotic in practice. Neighbouring starting points produce wildly different journeys, which you can see directly:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://analyticadss.com/img/posts/collatz/trajectory-lengths.webp&quot; alt=&quot;Total stopping time for each starting value: a jagged, self-similar skyline with no obvious order.&quot; width=&quot;828&quot; height=&quot;507&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;/&gt;&lt;/p&gt;
&lt;p&gt;That plot is the length of the sequence for each starting number. 26 takes ten steps; 27 takes 111. There’s structure in there: bands, echoes, a strange self-similarity. But nothing anyone has turned into a proof. The serious result we do have is Terence Tao’s from 2019: &lt;em&gt;almost all&lt;/em&gt; starting values eventually get almost as small as you like. “Almost all” is doing heavy lifting there. It’s the strongest statement anyone has managed, and it still doesn’t rule out one immortal orbit somewhere, spinning forever.&lt;/p&gt;
&lt;h2 id=&quot;run-it-yourself&quot;&gt;Run it yourself&lt;/h2&gt;
&lt;p&gt;The whole thing fits in a dozen lines of R:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;r&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;collatz&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; function&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;n&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;  steps&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; n&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  while&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;n&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; !=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;    n&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;n&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; %%&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ==&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;n&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; else&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 3&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; n&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;    steps&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; c&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;steps&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; n&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;  steps&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;collatz&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;27&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# 27 82 41 124 62 31 94 47 142 71 214 107 ... 4 2 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Trajectory lengths for the first 10,000 starting values&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;lengths&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; sapply&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;10000&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; function&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;n&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;length&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;collatz&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;n&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;plot&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;lengths&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; type&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &amp;quot;h&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; xlab&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &amp;quot;Starting value n&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;     ylab&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &amp;quot;Length of Collatz sequence&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Change the rule slightly, to 3n − 1 instead of 3n + 1, and you get cycles that never reach 1. So the conjecture is delicately balanced on its exact form, which somehow makes it worse.&lt;/p&gt;
&lt;p&gt;Why do I keep a soft spot for this one, alongside &lt;a href=&quot;https://analyticadss.com/insights/unsolved-math-problems-the-goldbach-conjecture/&quot;&gt;Goldbach&lt;/a&gt;? Because it’s the purest example I know of a lesson our whole field runs on: a system’s rules being simple tells you nothing about its behaviour being simple. Four lines of code, eighty years of failure. I show this to people who think complexity requires complication, and I watch it land every time.&lt;/p&gt;</content:encoded><dc:creator>Dr. Aous Abdo</dc:creator><category>Mathematics</category><category>R</category><category>Number theory</category></item><item><title>tidyverse and data.table, and when we reach for each</title><link>https://analyticadss.com/insights/the-tidyverse-and-data-table-r-packages/</link><guid isPermaLink="true">https://analyticadss.com/insights/the-tidyverse-and-data-table-r-packages/</guid><description>dplyr and data.table side by side on the same tasks, a measured group-by benchmark from 100,000 to 5 million rows, and a simple rule for choosing.</description><pubDate>Sun, 14 Feb 2021 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;em&gt;Written some years ago. The ideas hold, but check package versions and API details against current documentation before relying on the code.&lt;/em&gt;&lt;/p&gt;&lt;p&gt;Two R packages solve the same problem in opposite ways, and which one you should reach for depends on something most comparisons skip: how big the data is, and who else has to read the code.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;tidyverse&lt;/code&gt; optimises for the reader. &lt;code&gt;data.table&lt;/code&gt; optimises for the machine. Both are excellent. I use both, usually in the same project, and the choice is rarely close once you know what you’re optimising for.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;tidyverse&lt;/code&gt; is a family of packages, &lt;code&gt;dplyr&lt;/code&gt;, &lt;code&gt;tidyr&lt;/code&gt;, &lt;code&gt;ggplot2&lt;/code&gt; and others, built around a shared grammar. Verbs read left to right, joined by a pipe, and someone who has never seen the code can usually tell what a chain does on first reading. That’s not a small thing when the person maintaining your analysis in a year is not you.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;data.table&lt;/code&gt; trades that legibility for speed and memory. Its syntax is dense, it modifies in place rather than copying, and on a few million rows the difference stops being academic.&lt;/p&gt;
&lt;p&gt;Same operations in both, below.&lt;/p&gt;
&lt;hr/&gt;
&lt;h2 id=&quot;examples-tidyverse-examples&quot;&gt;Examples: Tidyverse Examples&lt;/h2&gt;
&lt;p&gt;Here are some examples of how to use the &lt;code&gt;tidyverse&lt;/code&gt;:&lt;/p&gt;
&lt;p&gt;To select specific columns from a dataset:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;r&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Load the tidyverse package&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;library&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;tidyverse&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Load the mpg dataset from the ggplot2 package&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;data&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;mpg&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Select the &amp;quot;manufacturer&amp;quot; and &amp;quot;model&amp;quot; columns&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;mpg&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; %&amp;gt;%&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; select&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;manufacturer&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; model&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And to group and summarize a dataset:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;r&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Load the tidyverse package&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;library&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;tidyverse&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Load the mpg dataset from the ggplot2 package&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;data&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;mpg&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Group the dataset by &amp;quot;class&amp;quot; and compute the mean of the &amp;quot;hwy&amp;quot; column&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;mpg&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; %&amp;gt;%&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; group_by&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;%&amp;gt;%&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; summarize&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;mean_hwy&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; mean&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;hwy&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To join two datasets:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;r&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Load the tidyverse package&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;library&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;tidyverse&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Load the mpg and cylinders datasets from the ggplot2 package&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;data&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;mpg&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;data&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;cylinders&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Join the mpg and cylinders datasets on the &amp;quot;manufacturer&amp;quot; column&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;mpg&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; %&amp;gt;%&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; left_join&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;cylinders&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; by&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &amp;quot;manufacturer&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To perform a linear regression using the &lt;code&gt;lm&lt;/code&gt; function from the &lt;code&gt;stats&lt;/code&gt; package:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;r&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Load the tidyverse and stats packages&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;library&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;tidyverse&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;library&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;stats&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Load the mtcars dataset&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;data&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;mtcars&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Perform a linear regression to predict mpg (miles per gallon) using wt (weight) as the predictor variable&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;fit&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; mtcars&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; %&amp;gt;%&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;  lm&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;mpg&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ~&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; wt&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; data&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; .&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Summarize the model results&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;summary&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;fit&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Create a scatterplot matrix using the &lt;code&gt;scatterplotMatrix&lt;/code&gt; function from the &lt;code&gt;car&lt;/code&gt; package:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;r&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Load the tidyverse and car packages&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;library&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;tidyverse&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;library&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;car&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Load the iris dataset&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;data&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;iris&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Create a scatterplot matrix of the iris dataset&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;scatterplotMatrix&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;iris&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; smooth&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; FALSE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Create a faceted bar plot using &lt;code&gt;ggplot2&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;r&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Load the tidyverse package&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;library&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;tidyverse&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Load the mpg dataset from the ggplot2 package&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;data&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;mpg&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Create a faceted bar plot showing the distribution of hwy (highway miles per gallon) by class and drv (drive type)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;ggplot&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;mpg&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; aes&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;x&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; hwy&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;  geom_histogram&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;binwidth&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;  facet_wrap&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;~&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; class&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; drv&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; nrow&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;hr/&gt;
&lt;h2 id=&quot;examples-datatable-examples&quot;&gt;Examples: data.table Examples&lt;/h2&gt;
&lt;p&gt;The &lt;code&gt;data.table&lt;/code&gt; package, on the other hand, is a high-performance package for working with large datasets. It provides functions for manipulating and querying data efficiently. The &lt;code&gt;data.table&lt;/code&gt; package is particularly useful when working with datasets that are too large to fit in memory or when you need to perform complex operations on large datasets.&lt;/p&gt;
&lt;h3 id=&quot;one-of-the-main-advantages-of-the-datatable-package&quot;&gt;One of the main advantages of the &lt;code&gt;data.table&lt;/code&gt; package&lt;/h3&gt;
&lt;p&gt;One of the main advantages of the &lt;code&gt;data.table&lt;/code&gt; package is its speed. The functions in the &lt;code&gt;data.table&lt;/code&gt; package are generally faster than their counterparts in the &lt;code&gt;tidyverse&lt;/code&gt;, especially when working with large datasets.&lt;/p&gt;
&lt;p&gt;So I measured it rather than repeating it. Group by a letter, average a column, three sizes:&lt;/p&gt;
&lt;p tabindex=&quot;0&quot; role=&quot;region&quot; aria-label=&quot;Figure&quot;&gt;&lt;img src=&quot;https://analyticadss.com/img/posts/tidyverse/groupby-measured.svg&quot; alt=&quot;Grouped bars comparing median run time for dplyr against data.table. At 100,000 rows it is 3 milliseconds against 2, a 1.5-fold gap. At 1 million rows, 21 against 13. At 5 million rows, 95 against 39 — a 2.4-fold gap.&quot; width=&quot;760&quot; height=&quot;380&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;/&gt;&lt;/p&gt;
&lt;p&gt;Both are fast, and that is the part the benchmark wars tend to skip. On a hundred thousand rows the difference is about a millisecond, so pick whichever you write more fluently. The gap only earns an opinion once the data stops being small, and then it widens rather than holding steady.&lt;/p&gt;
&lt;p&gt;Here are some more examples of how to use the &lt;code&gt;data.table&lt;/code&gt; package:&lt;/p&gt;
&lt;p&gt;To select specific columns from a dataset:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;r&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Load the data.table package&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;library&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;data.table&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Load the mpg dataset from the ggplot2 package&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;data&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;mpg&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Convert the dataset to a data.table&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;mpg&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; as.data.table&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;mpg&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Select the &amp;quot;manufacturer&amp;quot; and &amp;quot;model&amp;quot; columns&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;mpg&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; .&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;manufacturer&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; model&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and to group and summarize a dataset:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;r&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Load the data.table package&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;library&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;data.table&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Load the mpg dataset from the ggplot2 package&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;data&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;mpg&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Convert the dataset to a data.table&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;mpg&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; as.data.table&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;mpg&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Group the dataset by &amp;quot;class&amp;quot; and compute the mean of the &amp;quot;hwy&amp;quot; column&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;mpg&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; .&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;mean_hwy&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; mean&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;hwy&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; by&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; class&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To join two datasets:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;r&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Load the data.table package&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;library&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;data.table&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Load the mpg and cylinders datasets from the ggplot2 package&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;data&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;mpg&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;data&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;cylinders&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Convert the datasets to data.tables&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;mpg&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; as.data.table&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;mpg&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;cylinders&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; as.data.table&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;cylinders&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Join the mpg and cylinders datasets on the &amp;quot;manufacturer&amp;quot; column&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;mpg&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;cylinders&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; on&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &amp;quot;manufacturer&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Perform a linear regression using the &lt;code&gt;lm&lt;/code&gt; function from the &lt;code&gt;stats&lt;/code&gt; package and the &lt;code&gt;data.table&lt;/code&gt; package:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;r&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Load the data.table and stats packages&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;library&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;data.table&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;library&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;stats&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Load the mtcars dataset&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;data&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;mtcars&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Convert the dataset to a data.table&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;mtcars&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; setDT&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;mtcars&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Perform a linear regression to predict mpg (miles per gallon) using wt (weight) as the predictor variable&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;fit&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; mtcars&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; lm&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;mpg&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ~&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; wt&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Summarize the model results&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;summary&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;fit&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Create a scatterplot matrix using the &lt;code&gt;scatterplotMatrix&lt;/code&gt; function from the &lt;code&gt;car&lt;/code&gt; package and the &lt;code&gt;data.table&lt;/code&gt; package:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;r&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Load the data.table and car packages&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;library&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;data.table&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;library&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;car&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Load the iris dataset&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;data&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;iris&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Convert the dataset to a data.table&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;iris&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; as.data.table&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;iris&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Create a scatterplot matrix of the iris dataset&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;scatterplotMatrix&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;iris&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; smooth&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; FALSE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Create a faceted bar plot using &lt;code&gt;ggplot2&lt;/code&gt; and the &lt;code&gt;data.table&lt;/code&gt; package:&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;r&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Load the data.table and ggplot2 packages&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;library&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;data.table&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;library&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;ggplot2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Load the mpg dataset from the ggplot2 package&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;data&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;mpg&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Convert the dataset to a data.table&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;mpg&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; as.data.table&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;mpg&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Create a faceted bar plot showing the distribution of hwy (highway miles per gallon) by class and drv (drive type)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;ggplot&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;mpg&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; aes&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;x&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; hwy&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;  geom_histogram&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;binwidth&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;  facet_wrap&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;~&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; class&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; drv&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; nrow&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;hr/&gt;
&lt;p&gt;In terms of implementation, both the &lt;code&gt;tidyverse&lt;/code&gt; and &lt;code&gt;data.table&lt;/code&gt; packages are written in R, but some of the functions in the &lt;code&gt;data.table&lt;/code&gt; package are implemented in C for improved performance.&lt;/p&gt;
&lt;h2 id=&quot;so-which-one&quot;&gt;So which one?&lt;/h2&gt;
&lt;p&gt;Ask how big the data is, then ask who reads the code.&lt;/p&gt;
&lt;p&gt;Under a million rows, with people who will maintain it after you, use the &lt;code&gt;tidyverse&lt;/code&gt;. The chains read like sentences, the errors are legible, and the performance difference will not be what slows the project down. On a few million rows or more, &lt;code&gt;data.table&lt;/code&gt; stops being an optimisation and becomes the difference between an analysis that runs and one that doesn’t. It modifies in place instead of copying, and once memory is the constraint that matters more than syntax.&lt;/p&gt;
&lt;p&gt;Does it have to be one or the other? No. &lt;code&gt;dtplyr&lt;/code&gt; gives you &lt;code&gt;dplyr&lt;/code&gt; verbs on a &lt;code&gt;data.table&lt;/code&gt; backend, which is a reasonable middle if your team knows one syntax and your data has outgrown it.&lt;/p&gt;
&lt;h3 id=&quot;at-analytica&quot;&gt;At Analytica&lt;/h3&gt;
&lt;p&gt;We work in gigabytes to terabytes, so &lt;code&gt;data.table&lt;/code&gt; is the default here and the &lt;code&gt;tidyverse&lt;/code&gt; gets used for the plotting and the final report. That’s a consequence of our data sizes, not a verdict on the packages. Your constraint is probably different from ours, and it should decide this rather than anyone’s preference.&lt;/p&gt;</content:encoded><dc:creator>Dr. Aous Abdo</dc:creator><category>R</category><category>Data wrangling</category><category>Performance</category></item><item><title>Analyzing crypto markets in R, part 1: exchanges and arbitrage</title><link>https://analyticadss.com/insights/analyzing-cryptocurrency-markets-using-r-part-1/</link><guid isPermaLink="true">https://analyticadss.com/insights/analyzing-cryptocurrency-markets-using-r-part-1/</guid><description>Bitcoin&apos;s price wasn&apos;t one price. Pulling 2011–2018 exchange data into R and measuring the gaps arbitrage was supposed to close.</description><pubDate>Sat, 01 Dec 2018 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;em&gt;A 2018 analysis, kept for the method and the findings. The data feeds it used — Quandl&apos;s exchange series and the Poloniex API — have since shut down or changed beyond recognition, so the code is shown for its approach rather than to run as-is. The figures are the original outputs.&lt;/em&gt;&lt;/p&gt;&lt;p&gt;In 2018 I got curious about a question with real money attached: is Bitcoin’s price actually &lt;em&gt;one&lt;/em&gt; price? Every exchange quotes its own, arbitrage is supposed to keep them honest, and I had seven years of data to check whether it did.&lt;/p&gt;
&lt;p&gt;The toolkit was ordinary R: &lt;code&gt;data.table&lt;/code&gt; and &lt;code&gt;lubridate&lt;/code&gt; for wrangling, &lt;code&gt;ggplot2&lt;/code&gt; for the pictures, with price series pulled from Quandl’s exchange feeds and the Poloniex API. Those feeds are gone now, which is why this post carries the note above. The method outlives the plumbing.&lt;/p&gt;
&lt;p&gt;First, the shape of the thing we’re analysing:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://analyticadss.com/img/posts/crypto/btc-weighted-price-history.webp&quot; alt=&quot;Bitcoin&apos;s volume-weighted price, 2012–2018: years of near-flatline, then the vertical 2017 spike toward $20,000 and the fall after.&quot; width=&quot;828&quot; height=&quot;497&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;/&gt;&lt;/p&gt;
&lt;p&gt;That spike is late 2017, and everything interesting in this analysis happens in its shadow.&lt;/p&gt;
&lt;h2 id=&quot;how-far-apart-could-one-bitcoin-be-from-itself&quot;&gt;How far apart could one Bitcoin be from itself?&lt;/h2&gt;
&lt;p&gt;For each day I computed the maximum percentage gap between the highest and lowest price across exchanges — the raw material of arbitrage. Plotted over time, with a loess smoother:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://analyticadss.com/img/posts/crypto/price-difference-loess.webp&quot; alt=&quot;Daily maximum price difference between exchanges, log scale, 2013–2018, with a loess curve drifting down from several percent toward one percent.&quot; width=&quot;828&quot; height=&quot;492&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;/&gt;&lt;/p&gt;
&lt;p&gt;Two things jump out. The typical gap &lt;em&gt;declined&lt;/em&gt; over the years (the market maturing, arbitrageurs doing their job), but the spread never disappeared, and single days still spiked to 10% and beyond deep into the study. The monthly view says the same thing with distributions instead of dots:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://analyticadss.com/img/posts/crypto/monthly-price-difference-boxplots.webp&quot; alt=&quot;Box plots of price differences by month: medians settle near one percent, with fat upper tails in every volatile period.&quot; width=&quot;828&quot; height=&quot;499&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;/&gt;&lt;/p&gt;
&lt;p&gt;Medians settle around a percent. The tails never behave. And when I asked &lt;em&gt;which&lt;/em&gt; exchanges kept showing up at the extremes, the answer wasn’t random either:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://analyticadss.com/img/posts/crypto/exchange-max-min-differences.webp&quot; alt=&quot;Frequency of each exchange posting the day&apos;s maximum or minimum price: a small set of venues dominates both ends.&quot; width=&quot;828&quot; height=&quot;500&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;/&gt;&lt;/p&gt;
&lt;p&gt;A handful of venues were persistently the expensive place and a handful persistently the cheap one. Textbook arbitrage says gaps like that get eaten within minutes. So why did they persist for years?&lt;/p&gt;
&lt;p&gt;Because the friction was real: withdrawal limits and delays, fees on both sides, counterparty risk on venues of very mixed reliability, and transfer times during exactly the volatile moments when gaps opened. The chart records the size of the reward; the frictions were the moat around it. “Free money” that persists is almost always a measurement of some cost you haven’t priced.&lt;/p&gt;
&lt;p&gt;That finding — markets are one market only when moving between them is cheap — is the whole lesson, and it generalises far past crypto. &lt;a href=&quot;https://analyticadss.com/insights/analyzing-cryptocurrency-markets-using-r-part-2/&quot;&gt;Part 2&lt;/a&gt; turns from one coin on many exchanges to many coins moving together, where the correlation structure has its own surprise.&lt;/p&gt;</content:encoded><dc:creator>Dr. Aous Abdo</dc:creator><category>R</category><category>Finance</category><category>Data analysis</category></item><item><title>Analyzing crypto markets in R, part 2: everything correlates</title><link>https://analyticadss.com/insights/analyzing-cryptocurrency-markets-using-r-part-2/</link><guid isPermaLink="true">https://analyticadss.com/insights/analyzing-cryptocurrency-markets-using-r-part-2/</guid><description>The diversification that wasn&apos;t: daily returns for eight coins, correlation matrices, and what happens to independence when markets fall.</description><pubDate>Sat, 01 Dec 2018 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;em&gt;The 2018 companion piece to part 1, kept for the method and the findings. Its data feeds are gone, so the code is illustrative; the figures are the original outputs.&lt;/em&gt;&lt;/p&gt;&lt;p&gt;&lt;a href=&quot;https://analyticadss.com/insights/analyzing-cryptocurrency-markets-using-r-part-1/&quot;&gt;Part 1&lt;/a&gt; asked whether Bitcoin was one price across exchanges. This half asks a portfolio question: were the altcoins genuinely different assets, or eight tickers for the same bet?&lt;/p&gt;
&lt;p&gt;The 2018 altcoin pitch leaned on diversification — don’t just hold BTC, spread across ETH, LTC, XRP and friends. Whether that spreads &lt;em&gt;risk&lt;/em&gt; depends entirely on correlation, and correlation is measurable. Same toolkit as before, prices for eight major coins, everything converted to daily returns:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://analyticadss.com/img/posts/crypto/daily-returns-all-pairs.webp&quot; alt=&quot;Daily percentage returns for eight coin pairs, 2015–2018: quiet stretches punctuated by synchronized spikes across every panel at once.&quot; width=&quot;828&quot; height=&quot;492&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;/&gt;&lt;/p&gt;
&lt;p&gt;Even before any statistics, your eye catches it: the spikes line up. When one panel goes wild, they all do. The correlation matrix makes it precise:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://analyticadss.com/img/posts/crypto/correlation-matrix-ellipses.webp&quot; alt=&quot;Correlation matrix of daily returns for XRP, XEM, ETH, XMR, LTC, DASH, BCH and BTC: almost everything between 0.7 and 0.95.&quot; width=&quot;826&quot; height=&quot;722&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;/&gt;&lt;/p&gt;
&lt;p&gt;Nearly every pair sits between 0.7 and 0.95. In equity terms those are numbers you’d expect &lt;em&gt;within one sector&lt;/em&gt;, not across an asset class. Drawn as a network, with distance standing for independence, the “diverse” portfolio collapses into one tight tangle:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://analyticadss.com/img/posts/crypto/static-correlation-network.webp&quot; alt=&quot;Static correlation network, 2014–2018: eight coins knotted into a single cluster, with BCH the only partial outlier.&quot; width=&quot;828&quot; height=&quot;519&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;/&gt;&lt;/p&gt;
&lt;p&gt;Eight names, one knot. Holding five of these wasn’t diversification; it was the same position with extra fees.&lt;/p&gt;
&lt;h2 id=&quot;the-part-that-should-worry-a-portfolio&quot;&gt;The part that should worry a portfolio&lt;/h2&gt;
&lt;p&gt;Averages hide the dangerous detail, so I rolled the BTC–LTC correlation month by month:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://analyticadss.com/img/posts/crypto/btc-ltc-correlation-over-time.webp&quot; alt=&quot;Correlation between BTC and LTC daily returns computed per month, 2015–2018: mostly 0.7–0.9, dipping only in quiet periods.&quot; width=&quot;828&quot; height=&quot;519&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;/&gt;&lt;/p&gt;
&lt;p&gt;The dips toward independence happen in calm stretches. In stressed ones the correlation pins to the top of the range — and the 2018 price panels show what that meant in practice:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://analyticadss.com/img/posts/crypto/altcoin-price-facets.webp&quot; alt=&quot;2018 prices for eight coins, faceted: every panel is the same descending shape.&quot; width=&quot;828&quot; height=&quot;499&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;/&gt;&lt;/p&gt;
&lt;p&gt;Eight charts, one shape, all pointing down together. Which is the general and much older lesson this data illustrates unusually cleanly: &lt;strong&gt;correlations rise exactly when you need them low.&lt;/strong&gt; Diversification quoted from peaceful averages evaporates in a crisis, in crypto and everywhere else — 2008 taught equity investors the same thing at greater expense.&lt;/p&gt;
&lt;p&gt;Would I expect identical numbers today? No; the market has had years to mature, and I’d want fresh data before claiming anything about now. But the &lt;em&gt;method&lt;/em&gt; is the reusable part: returns, not prices; correlations over time, not on average; and a hard look at exactly the periods your model is most tempted to smooth over.&lt;/p&gt;</content:encoded><dc:creator>Dr. Aous Abdo</dc:creator><category>R</category><category>Finance</category><category>Data analysis</category></item><item><title>Conway&apos;s Game of Life, in R and in Python</title><link>https://analyticadss.com/insights/conways-game-of-life-with-examples-in-r-and-python/</link><guid isPermaLink="true">https://analyticadss.com/insights/conways-game-of-life-with-examples-in-r-and-python/</guid><description>The four rules of Conway&apos;s Game of Life, the patterns they produce (gliders, oscillators, guns), and a full simulation in Python and in R to run yourself.</description><pubDate>Wed, 17 Jan 2018 00:00:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;em&gt;Written some years ago. The ideas hold, but check package versions and API details against current documentation before relying on the code.&lt;/em&gt;&lt;/p&gt;&lt;p&gt;Four rules. No player. That’s the entire specification of Conway’s Game of Life, and it is enough to build a pattern that computes.&lt;/p&gt;
&lt;p&gt;John Conway published it in 1970. It’s a cellular automaton: a grid where each cell is alive or dead, and every cell’s next state depends only on how many of its eight neighbours are currently alive. You set a starting arrangement and then you don’t touch it again. Everything after that is the rules running.&lt;/p&gt;
&lt;p&gt;What makes it worth an afternoon is how little it takes to get complicated. Gliders that travel across the grid. Oscillators that return to their starting shape every few generations. Arrangements that grow without bound. People have built a working Turing machine out of it, which means the four rules below are, in a strict sense, capable of computing anything computable.&lt;/p&gt;
&lt;p&gt;Below: the rules, then implementations in R and in Python, both short enough to read in one sitting.&lt;/p&gt;
&lt;h2 id=&quot;game-rules&quot;&gt;Game Rules&lt;/h2&gt;
&lt;p&gt;The game rules are as follows:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Any live cell with fewer than two live neighbors dies, as if by underpopulation.&lt;/li&gt;
&lt;li&gt;Any live cell with two or three live neighbors lives on to the next generation.&lt;/li&gt;
&lt;li&gt;Any live cell with more than three live neighbors dies, as if by overpopulation.&lt;/li&gt;
&lt;li&gt;Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;These rules are applied to each cell in the grid simultaneously, with the state of the cells in the next generation being determined based on the state of the cells in the current generation. The game continues in this way, with the state of the cells being updated in each generation.&lt;/p&gt;
&lt;p&gt;The Game of Life can be implemented in many different ways, with the rules being applied to a two-dimensional grid, or to a one-dimensional “tape,” or even to a three-dimensional space. There are also many variations of the rules that have been explored, with different sets of rules leading to different patterns and behaviors in the game.&lt;/p&gt;
&lt;p&gt;The Game of Life is capable of producing a wide range of patterns, depending on the initial configuration of the cells and the specific rules that are being used. Some patterns remain stable over time, while others may evolve and change over the course of the game.&lt;/p&gt;
&lt;h2 id=&quot;game-patterns&quot;&gt;Game Patterns&lt;/h2&gt;
&lt;p&gt;The classic starting shapes and their fates, from the original post:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://analyticadss.com/img/posts/conway/pattern-taxonomy.webp&quot; alt=&quot;A taxonomy of small starting patterns: several settle into stable blocks, one dies out entirely, and one repeats forever.&quot; width=&quot;828&quot; height=&quot;877&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;/&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://analyticadss.com/img/posts/conway/glider-and-friends.webp&quot; alt=&quot;Familiar residents of the grid, including the glider — the small arrangement that travels diagonally forever.&quot; width=&quot;610&quot; height=&quot;178&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot;/&gt;&lt;/p&gt;
&lt;p&gt;Few examples of patterns that can appear in the Game of Life&lt;/p&gt;
&lt;p&gt;Here are a few examples of patterns that can appear in the Game of Life:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Still lifes: These are patterns that remain stable over time and do not change from one generation to the next. Examples of still lifes include blocks, beehives, and loafs.&lt;/li&gt;
&lt;li&gt;Oscillators: These are patterns that repeat themselves over time, cycling through a fixed set of states. Examples of oscillators include blinkers, toads, and pulsars.&lt;/li&gt;
&lt;li&gt;Spaceships: These are patterns that move across the grid, leaving behind a “trail” of cells as they go. Spaceships can move in any of the four cardinal directions, and their movement may be periodic or aperiodic.&lt;/li&gt;
&lt;li&gt;Gliders: These are a type of spaceship that move diagonally across the grid, leaving behind a distinctive “trail” of cells. Gliders are one of the simplest and most well-known spaceships in the Game of Life.&lt;/li&gt;
&lt;li&gt;Guns: These are patterns that produce an endless stream of spaceships or other patterns. The first gun was discovered in 1970 by Bill Gosper, and many more have been found since.&lt;/li&gt;
&lt;li&gt;Patterns with complex behavior: Some patterns in the Game of Life exhibit behavior that is difficult to predict or understand. These patterns may exhibit seemingly random behavior, or they may exhibit complex, self-organizing behavior.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;These are just a few examples of the patterns that can appear in the Game of Life. The game is capable of producing a wide range of patterns and behaviors, and new patterns are still being discovered today.&lt;/p&gt;
&lt;h2 id=&quot;real-life-examples&quot;&gt;Real-Life Examples&lt;/h2&gt;
&lt;p&gt;So does any of this matter outside a screensaver? More than I expected when I first met it. The Game of Life turns up wherever people are trying to understand how local rules produce global behaviour.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Biology: The Game of Life has been used to model the behavior of cellular automata, including the growth and division of cells. It has also been used to study patterns in the distribution of species in ecosystems and the spread of epidemics.&lt;/li&gt;
&lt;li&gt;Physics: The Game of Life has been used to model the behavior of physical systems, such as the formation of patterns in crystals and the behavior of fluids.&lt;/li&gt;
&lt;li&gt;Computer science: The Game of Life has been used as a test bed for exploring algorithms and data structures, and it has inspired the development of a number of computational models and techniques.&lt;/li&gt;
&lt;li&gt;Art and design: The Game of Life has inspired a number of artistic and design projects, including digital art, installations, and interactive exhibits.&lt;/li&gt;
&lt;li&gt;Education: The Game of Life has been used as a tool for teaching concepts in mathematics, computer science, and other fields, and it has been incorporated into educational software and curricula.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The thread running through all of those is the same one: nobody designed the glider. It falls out of the rules. That is the property people keep coming back for, and it is why I think every analyst should implement this once by hand.&lt;/p&gt;
&lt;h2 id=&quot;game-simulation-in-r&quot;&gt;Game Simulation in R&lt;/h2&gt;
&lt;p&gt;The script below simulates the Game of Life and produces a plot of the evolution of the cells over time. The &lt;code&gt;plotly&lt;/code&gt; package is used to create an interactive heatmap, with “dead” cells being shown in darker shades and “alive” cells being shown in lighter shades. The x-axis of the plot represents the generation, and the y-axis represents the cell.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;r&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Install the plotly and furrr packages if they are not already installed&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# install.packages(&amp;quot;plotly&amp;quot;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# install.packages(&amp;quot;furrr&amp;quot;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;library&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;plotly&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;library&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;furrr&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Set up the grid for the game&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;grid&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; matrix&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;sample&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;c&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 100&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; replace&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; TRUE&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; nrow&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 100&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Initialize a list to store the state of the cells at each generation&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;grid_history&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; rep&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;list&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;grid&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; generations&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Simulate the game using the furrr package to parallelize the simulation&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;plan&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;multisession&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;grid_history&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; future_map&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;generations&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; function&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;  # Get the current state of the cells&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;  current_state&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; grid_history&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;[[&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;  # Initialize the next state of the cells&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;  next_state&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; matrix&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; nrow&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; nrow&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;current_state&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; ncol&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; ncol&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;current_state&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;  # Iterate over each cell in the grid&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;  for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;x&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;nrow&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;current_state&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;y&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;ncol&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;current_state&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;      # Get the number of live neighbors for the current cell&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;      neighbors&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; sum&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;current_state&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;max&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;x&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;min&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;x&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; nrow&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;current_state&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;                                     max&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;y&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;min&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;y&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; ncol&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;current_state&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))]) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; current_state&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;x&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; y&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;      # Apply the rules of the Game of Life to determine the next state of the cell&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;      if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;current_state&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;x&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; y&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;neighbors&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ||&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; neighbors&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;          next_state&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;x&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; y&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        } &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;else&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;          next_state&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;x&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; y&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      } &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;else&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;neighbors&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; ==&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;          next_state&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;x&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; y&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        } &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;else&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;          next_state&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;x&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; y&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;      }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;  # Update the state of the cells&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;  grid&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; &amp;lt;-&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; next_state&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;  # Store the state of the cells at the current generation&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;  grid&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;})&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Plot the evolution of the cells over time using the plotly package&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;plot_ly&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;z&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; grid_history&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; colorscale&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &amp;quot;Blackbody&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; type&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &amp;quot;heatmapgl&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;%&amp;gt;%&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#B392F0&quot;&gt;  layout&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;xaxis&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; list&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;title&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &amp;quot;Generation&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt; yaxis&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#B392F0&quot;&gt; list&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;title&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt; &amp;quot;Cell&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;game-simulation-in-python&quot;&gt;Game Simulation in Python&lt;/h2&gt;
&lt;p&gt;The script below simulates the Game of Life and produces a plot of the evolution of the cells over time. The &lt;code&gt;matplotlib&lt;/code&gt; package is used to create a heatmap, with “dead” cells being shown in darker shades and “alive” cells being shown in lighter shades. The x-axis of the plot represents the generation, and the y-axis represents the cell.&lt;/p&gt;
&lt;pre class=&quot;astro-code github-dark&quot; style=&quot;background-color:#24292e;color:#e1e4e8;overflow-x:auto&quot; tabindex=&quot;0&quot; data-language=&quot;python&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; numpy &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; np&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; matplotlib.pyplot &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;as&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; plt&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Set up the grid for the game&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;grid &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; np.random.choice([&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;], &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;size&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Initialize a list to store the state of the cells at each generation&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;grid_history &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; [grid]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Simulate the game&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; i &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; range&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(generations):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;    # Get the current state of the cells&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    current_state &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; grid_history[i]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;    # Initialize the next state of the cells&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    next_state &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; np.zeros((&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;    # Iterate over each cell in the grid&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; x &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; range&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;        for&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; y &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;in&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; range&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;            # Get the number of live neighbors for the current cell&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;            neighbors &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; (current_state[&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;max&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(x&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;):&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;min&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(x&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;), &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;max&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(y&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;):&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;min&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;(y&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)]).sum() &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; current_state[x, y]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;            # Apply the rules of the Game of Life to determine the next state of the cell&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;            if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; current_state[x, y] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; neighbors &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt; or&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; neighbors &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;                    next_state[x, y] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                else&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;                    next_state[x, y] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;            else&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                if&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; neighbors &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 3&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;                    next_state[x, y] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#F97583&quot;&gt;                else&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;                    next_state[x, y] &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#79B8FF&quot;&gt; 0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;    # Update the state of the cells&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    grid &lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt; next_state&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;    # Store the state of the cells at the current generation&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;    grid_history.append(grid)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#959DA5&quot;&gt;# Plot the evolution of the cells over time using the matplotlib package&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;plt.imshow(grid_history, &lt;/span&gt;&lt;span style=&quot;color:#FFAB70&quot;&gt;cmap&lt;/span&gt;&lt;span style=&quot;color:#F97583&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&amp;quot;Greys&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;plt.xlabel(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&amp;quot;Generation&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;plt.ylabel(&lt;/span&gt;&lt;span style=&quot;color:#9ECBFF&quot;&gt;&amp;quot;Cell&amp;quot;&lt;/span&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;color:#E1E4E8&quot;&gt;plt.show()&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&quot;why-its-worth-your-time&quot;&gt;Why it’s worth your time&lt;/h2&gt;
&lt;p&gt;Four rules and no player, and you get gliders, oscillators, and arrangements that grow forever. That gap between how simple the specification is and how complicated the behaviour gets is the whole reason people still write these implementations forty-odd years on.&lt;/p&gt;
&lt;p&gt;It’s also a useful thing to keep in mind when you build models for a living. Complicated output does not require a complicated model. Sometimes it requires four rules and enough iterations, which is worth remembering the next time somebody insists a phenomenon must have a complicated cause.&lt;/p&gt;
&lt;p&gt;Run either script above and change the starting grid. That’s the fun part.&lt;/p&gt;</content:encoded><dc:creator>Dr. Aous Abdo</dc:creator><category>R</category><category>Python</category><category>Simulation</category></item></channel></rss>