<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/you-beautiful-naive-sophisticated-newborn-series/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.masalmon.eu/2017/11/05/newborn-serie/"> Maëlle</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>My husband and I recently started watching the wonderful series “Parks and recreation” which was recommended to me by my fellow R-Lady Jennifer Thompson in <a rel="nofollow" href="https://twitter.com/jent103/status/908162485689122816" target="_blank">this very convincing thread</a>. The serie was even endorsed by other R-Ladies. Jennifer told me the first two seasons are not as good as the following ones, but that it was worth it to make it through them. We actually started enjoying the humor and characters right away!</p>
<p>Then, this week while watching the show, one of the characters did a very basic text analysis that made me feel like imitating him for a blog post – my husband told me it was very Leslie of me to plan something while doing something else which made me very proud. I <a rel="nofollow" href="https://twitter.com/ma_salmon/status/926167897478778880" target="_blank">tested my idea</a> on other Leslie fans, and they seemed to think it was a great idea… and that this post should be the beginning of a series of R-Ladies blog posts about Parks and recreation!</p>
<p>In this two-short-part blog post, I’ll therefore inaugurate this series, what an honor!</p>
<p><span></span></p>
<h1>Less than stellar first seasons?</h1>
<p>Jennifer told me the first two seasons were not the best ones. My brother who is a cruel man (just kidding) told me this was a very good joke idea: you tell someone a very bad TV series starts getting awesome after a few seasons that you need to watch in order to know the characters… therefore forcing them to loose their time and sanity in front of the screen, ah! Luckily Jennifer wasn’t doing that.</p>
<p>I said my husband and I were hooked on the series right from the first episode… what about other people? To answer this question, I downloaded <a rel="nofollow" href="http://www.imdb.com/" target="_blank">IMDB</a> ratings for the show! I first tried using R packages accessing the <a rel="nofollow" href="http://www.omdbapi.com/" target="_blank">OMDB API</a>, but <a rel="nofollow" href="https://github.com/RMHogervorst/imdb" target="_blank">this one</a> was outdated and <a rel="nofollow" href="https://github.com/hrbrmstr/omdbapi" target="_blank">this up-to-date one</a> was not but taught me the API no longer returns ratings by season… So I scraped the IMDB website using the <a rel="nofollow" href="https://github.com/hadley/rvest" target="_blank"><code>rvest</code> package</a>, after checking I was allowed to do so thanks to the <a rel="nofollow" href="https://github.com/ropenscilabs/robotstxt" target="_blank"><code>robotstxt</code> package</a>.</p>
<div>
<div>
<pre><code><span>robotstxt</span><span>::</span><span>paths_allowed</span><span>(</span><span>&quot;http://www.imdb.com/title/tt1266020/eprate&quot;</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>## [1] TRUE
</code></pre>
</div>
</div>
<div>
<div>
<pre><code><span>library</span><span>(</span><span>&quot;rvest&quot;</span><span>)</span><span>
</span><span>imdb_page</span><span> </span><span>&lt;-</span><span> </span><span>html</span><span>(</span><span>&quot;http://www.imdb.com/title/tt1266020/eprate&quot;</span><span>)</span><span>
</span><span>ratings</span><span> </span><span>&lt;-</span><span> </span><span>imdb_page</span><span> </span><span>%>%</span><span>
</span><span>html_nodes</span><span>(</span><span>&quot;table&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>.</span><span>[[</span><span>1</span><span>]]</span><span> </span><span>%>%</span><span>
</span><span>html_table</span><span>()</span><span> </span><span>%>%</span><span>
</span><span>.</span><span>[,</span><span>1</span><span>:</span><span>4</span><span>]</span><span>
</span><span>knitr</span><span>::</span><span>kable</span><span>(</span><span>ratings</span><span>[</span><span>1</span><span>:</span><span>5</span><span>,])</span><span>
</span></code></pre>
</div>
</div>
<table>
<thead>
<tr>
<th>#</th>
<th>Episode</th>
<th>UserRating</th>
<th>UserVotes</th>
</tr>
</thead>
<tbody>
<tr>
<td>7.13</td>
<td>One Last Ride: Part 2</td>
<td>9.7</td>
<td>2,407</td>
</tr>
<tr>
<td>7.12</td>
<td>One Last Ride: Part 1</td>
<td>9.6</td>
<td>1,934</td>
</tr>
<tr>
<td>7.40</td>
<td>Leslie and Ron</td>
<td>9.6</td>
<td>1,842</td>
</tr>
<tr>
<td>6.22</td>
<td>Moving Up: Part 2</td>
<td>9.4</td>
<td>1,273</td>
</tr>
<tr>
<td>5.14</td>
<td>Leslie and Ben</td>
<td>9.3</td>
<td>1,205</td>
</tr>
</tbody>
</table>
<p>I just had to wrangle this table a little bit.</p>
<div>
<div>
<pre><code><span>names</span><span>(</span><span>ratings</span><span>)[</span><span>1</span><span>]</span><span> </span><span>&lt;-</span><span> </span><span>&quot;number&quot;</span><span>
</span><span>ratings</span><span> </span><span>&lt;-</span><span> </span><span>tidyr</span><span>::</span><span>separate</span><span>(</span><span>ratings</span><span>,</span><span> </span><span>col</span><span> </span><span>=</span><span> </span><span>number</span><span>,</span><span>
</span><span>into</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;season&quot;</span><span>,</span><span> </span><span>&quot;episode&quot;</span><span>),</span><span>
</span><span>sep</span><span> </span><span>=</span><span> </span><span>&quot;\\.&quot;</span><span>)</span><span>
</span><span>ratings</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>mutate</span><span>(</span><span>ratings</span><span>,</span><span> </span><span>UserVotes</span><span> </span><span>=</span><span> </span><span>stringr</span><span>::</span><span>str_replace</span><span>(</span><span>UserVotes</span><span>,</span><span> </span><span>&quot;,&quot;</span><span>,</span><span> </span><span>&quot;&quot;</span><span>))</span><span>
</span><span>ratings</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>mutate</span><span>(</span><span>ratings</span><span>,</span><span> </span><span>UserVotes</span><span> </span><span>=</span><span> </span><span>as.numeric</span><span>(</span><span>UserVotes</span><span>))</span><span>
</span><span>ratings</span><span> </span><span>&lt;-</span><span> </span><span>dplyr</span><span>::</span><span>mutate</span><span>(</span><span>ratings</span><span>,</span><span> </span><span>episode</span><span> </span><span>=</span><span> </span><span>as.numeric</span><span>(</span><span>episode</span><span>))</span><span>
</span></code></pre>
</div>
</div>
<p>And then I could at last plot the ratings and see whether the two first seasons were not that loved! Before that I quickly assessed whether there was an ok number of votes for all episodes, and that this did not vary widely over seasons, which was the case.</p>
<div>
<div>
<pre><code><span>library</span><span>(</span><span>&quot;ggplot2&quot;</span><span>)</span><span>
</span><span>library</span><span>(</span><span>&quot;hrbrthemes&quot;</span><span>)</span><span>
</span><span>ggplot</span><span>(</span><span>ratings</span><span>,</span><span> </span><span>aes</span><span>(</span><span>episode</span><span>,</span><span> </span><span>UserRating</span><span>,</span><span>
</span><span>color</span><span> </span><span>=</span><span> </span><span>season</span><span>))</span><span> </span><span>+</span><span>
</span><span>geom_smooth</span><span>()</span><span>+</span><span>
</span><span>geom_point</span><span>()</span><span> </span><span>+</span><span>
</span><span>xlab</span><span>(</span><span>&quot;IMDB rating&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>viridis</span><span>::</span><span>scale_color_viridis</span><span>(</span><span>discrete</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span> </span><span>+</span><span>
</span><span>theme_ipsum</span><span>(</span><span>base_size</span><span> </span><span>=</span><span> </span><span>20</span><span>,</span><span>
</span><span>axis_title_size</span><span> </span><span>=</span><span> </span><span>20</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<p><img alt="plot of chunk unnamed-chunk-3" src="https://i0.wp.com/www.masalmon.eu/figure/source/2017-11-05-newborn-serie/unnamed-chunk-3-1.png?w=456" data-recalc-dims="1"></p>
<p>Note that for the first season, there are too few points for actually doing a smoothing. Looking at the plot I guess we can say the first season with its only 6 episodes was slightly less enjoyed! I am very thankful that the first season was deemed good enough to continue the series though! Besides I am thankful that it was so simple to answer this crucial question.</p>
<h1>Leslie’s wordcloud</h1>
<p>Warning: the wordcloud produced here might be a spoiler if you have not watched the 4 first seasons!</p>
<p>In the 10th episode of season 4, Tom produces<a rel="nofollow" href="http://ifiwasthelastgirl.tumblr.com/post/77940482792/can-we-talk-about-leslies-word-cloud" target="_blank"> a wordcloud of Leslie’s emails and memos</a>. I cannot really do the same, but was inspired to make a worldcloud of things said in the episodes by downloading all subtitles from the 4 first seasons and using the fantastic <a rel="nofollow" href="https://github.com/fkeck/subtools" target="_blank"><code>subtools</code> package</a>. Note that a worldcloud is probably not the coolest thing one can do with all these text data… but it is a start and I truly hope it inspires more people to use François Keck’s package which is really user-friendly since it can read all the subtitles of a serie at once from a folder where each sub-folder is a season. It also supports conversion to formats suitable for text analysis!</p>
<p>The single problem I experienced when doing this work was that I had to download subtitles from the internet and the websites where you find those are not cute places, they’re full of “women to date”…</p>
<p>To make the wordcloud I very simply followed <a rel="nofollow" href="http://www.pieceofk.fr/?p=437" target="_blank">this blog post of François Keck’s</a>. It doesn’t get easier than that… which is great given I have very little time at the moment.</p>
<div>
<div>
<pre><code><span>library</span><span>(</span><span>&quot;subtools&quot;</span><span>)</span><span>
</span><span>library</span><span>(</span><span>&quot;tm&quot;</span><span>)</span><span>
</span><span>library</span><span>(</span><span>&quot;SnowballC&quot;</span><span>)</span><span>
</span><span>library</span><span>(</span><span>&quot;wordcloud&quot;</span><span>)</span><span>
</span><span>parks_and_rec</span><span> </span><span>&lt;-</span><span> </span><span>read.subtitles.serie</span><span>(</span><span>dir</span><span> </span><span>=</span><span> </span><span>paste0</span><span>(</span><span>getwd</span><span>(),</span><span> </span><span>&quot;/data/pr/&quot;</span><span>))</span><span>
</span></code></pre>
</div>
</div>
<div>
<div>
<pre><code>## Read: 4 seasons, 68 episodes
</code></pre>
</div>
</div>
<div>
<div>
<pre><code><span>corpus</span><span> </span><span>&lt;-</span><span> </span><span>tmCorpus</span><span>(</span><span>parks_and_rec</span><span>)</span><span>
</span><span>corpus</span><span> </span><span>&lt;-</span><span> </span><span>tm_map</span><span>(</span><span>corpus</span><span>,</span><span> </span><span>content_transformer</span><span>(</span><span>tolower</span><span>))</span><span>
</span><span>corpus</span><span> </span><span>&lt;-</span><span> </span><span>tm_map</span><span>(</span><span>corpus</span><span>,</span><span> </span><span>removePunctuation</span><span>)</span><span>
</span><span>corpus</span><span> </span><span>&lt;-</span><span> </span><span>tm_map</span><span>(</span><span>corpus</span><span>,</span><span> </span><span>removeNumbers</span><span>)</span><span>
</span><span>corpus</span><span> </span><span>&lt;-</span><span> </span><span>tm_map</span><span>(</span><span>corpus</span><span>,</span><span> </span><span>removeWords</span><span>,</span><span> </span><span>stopwords</span><span>(</span><span>&quot;english&quot;</span><span>))</span><span>
</span><span>corpus</span><span> </span><span>&lt;-</span><span> </span><span>tm_map</span><span>(</span><span>corpus</span><span>,</span><span> </span><span>stripWhitespace</span><span>)</span><span>
</span><span>TDM</span><span> </span><span>&lt;-</span><span> </span><span>TermDocumentMatrix</span><span>(</span><span>corpus</span><span>)</span><span>
</span><span>TDM</span><span> </span><span>&lt;-</span><span> </span><span>as.matrix</span><span>(</span><span>TDM</span><span>)</span><span>
</span><span>vec.season</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>rep</span><span>(</span><span>1</span><span>,</span><span> </span><span>6</span><span>),</span><span> </span><span>rep</span><span>(</span><span>2</span><span>,</span><span> </span><span>24</span><span>),</span><span> </span><span>rep</span><span>(</span><span>3</span><span>,</span><span> </span><span>16</span><span>),</span><span> </span><span>rep</span><span>(</span><span>4</span><span>,</span><span> </span><span>22</span><span>))</span><span>
</span><span>TDM.season</span><span> </span><span>&lt;-</span><span> </span><span>t</span><span>(</span><span>apply</span><span>(</span><span>TDM</span><span>,</span><span> </span><span>1</span><span>,</span><span> </span><span>function</span><span>(</span><span>x</span><span>)</span><span> </span><span>tapply</span><span>(</span><span>x</span><span>,</span><span> </span><span>vec.season</span><span>,</span><span> </span><span>sum</span><span>)))</span><span>
</span><span>colnames</span><span>(</span><span>TDM.season</span><span>)</span><span> </span><span>&lt;-</span><span> </span><span>paste</span><span>(</span><span>&quot;S&quot;</span><span>,</span><span> </span><span>1</span><span>:</span><span>4</span><span>)</span><span>
</span><span>set.seed</span><span>(</span><span>1</span><span>)</span><span>
</span><span>comparison.cloud</span><span>(</span><span>TDM.season</span><span>,</span><span> </span><span>title.size</span><span> </span><span>=</span><span> </span><span>1</span><span>,</span><span> </span><span>max.words</span><span> </span><span>=</span><span> </span><span>100</span><span>,</span><span> </span><span>random.order</span><span> </span><span>=</span><span> </span><span>T</span><span>)</span><span>
</span></code></pre>
</div>
</div>
<p><img alt="plot of chunk unnamed-chunk-4" src="https://i1.wp.com/www.masalmon.eu/figure/source/2017-11-05-newborn-serie/unnamed-chunk-4-1.png?w=456" data-recalc-dims="1"></p>
<p>It doesn’t look anything like <a rel="nofollow" href="http://ifiwasthelastgirl.tumblr.com/post/77940482792/can-we-talk-about-leslies-word-cloud" target="_blank">Leslie’s wordcloud</a> but it made me happy because I had already forgotten about some words or names from the previous seasons!</p>
<p>Another thing I did with the subtitles was filtering those starting with “Oh Ann” in order to have a look at some of the terrific compliments Leslie made to Ann… And one of them inspired the title of this post, replacing “baby” by “series”!</p>
<h1>The end of this post, the beginning a series!</h1>
<p>Now that I’ve done my homework I’m truly looking forward to reading the other posts. I can’t wait! Stay tuned by following my fellow bloggers/Leslie fans who came out <a rel="nofollow" href="https://twitter.com/ma_salmon/status/926167897478778880" target="_blank">in this Twitter thread</a>.</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.masalmon.eu/2017/11/05/newborn-serie/"> Maëlle</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/D5ZiTJOLjoI" height="1"></a></div>