<div> I recently learned that you can download all of your Facebook data, so I decided to check it out and bring it into R. To access your data, go to Facebook, and click on the white down arrow in the upper-right corner. From there, select Settings, then, from the column on the left, “Your Facebook Information.” When you get the Facebook Information screen, select “View” next to “Download Your Information.” On this screen, you’ll be able to select the kind of data you want, a date range, and format. I only wanted my posts, so under “Your Information,” I deselected everything but the first item on the list, “Posts.” (Note that this will still download all photos and videos you posted, so it will be a large file.) To make it easy to bring into R, I selected JSON under Format (the other option is HTML).
<div><a rel="nofollow" href="https://i1.wp.com/4.bp.blogspot.com/-wpJRUjOGBlM/WyLRUp1uPlI/AAAAAAAAL1g/RzYTs8aHUnACbWqf-tumRgR143CG_IS6wCLcBGAs/s1600/download_data.png?ssl=1" target="_blank"><img border="0" src="https://i1.wp.com/4.bp.blogspot.com/-wpJRUjOGBlM/WyLRUp1uPlI/AAAAAAAAL1g/RzYTs8aHUnACbWqf-tumRgR143CG_IS6wCLcBGAs/s320/download_data.png?resize=320%2C140&ssl=1" data-original-width="450" width="320" data-original-height="702" data-recalc-dims="1" height="140"></a></div>
<p>After you click “Create File,” it will take a while to compile – you’ll get an email when it’s ready. You’ll need to reenter your password when you go to download the file.</p>
<p>The result is a Zip file, which contains folders for Posts, Photos, and Videos. Posts includes your own posts (on your and others’ timelines) as well as posts from others on your timeline. And, of course, the file needed a bit of cleaning. Here’s what I did.</p>
<p>Since the post data is a JSON file, I need the jsonlite package to read it.</p>
<div>
<div>
<div>
<pre><span>setwd</span><span>(</span><span>&quot;C:/Users/slocatelli/Downloads/facebook-saralocatelli35/posts&quot;</span><span>)</span><br><span>library</span><span>(jsonlite)</span><br><br><span>FBposts</span> <span>&lt;-</span> <span>fromJSON</span><span>(</span><span>&quot;your_posts.json&quot;</span><span>)</span><br></pre>
</div>
</div>
</div>
<p>This creates a large list object, with my data in a data frame. So as I did with the <a rel="nofollow" href="http://www.deeplytrivial.com/2018/05/how-has-taylor-swifts-word-choice.html" target="_blank">Taylor Swift albums</a>, I can pull out that data frame.</p>
<div>
<div>
<div>
<pre><span>myposts</span> <span>&lt;-</span> <span>FBposts</span><span>$</span><span>status_updates</span><br></pre>
</div>
</div>
</div>
<p>The resulting data frame has 5 columns: timestamp, which is in UNIX format; attachments, any photos, videos, URLs, or Facebook events attached to the post; title, which always starts with the author of the post (you or your friend who posted on your timeline) followed by the type of post; data, the text of the post; and tags, the people you tagged in the post.</p>
<p>First, I converted the timestamp to datetime, using the anytime package.</p>
<div>
<div>
<div>
<pre><span>library</span><span>(anytime)</span><br><br><span>myposts</span><span>$</span><span>timestamp</span> <span>&lt;-</span> <span>anytime</span><span>(myposts</span><span>$</span><span>timestamp)</span><br></pre>
</div>
</div>
</div>
<p>Next, I wanted to pull out post author, so that I could easily filter the data frame to only use my own posts.</p>
<div>
<div>
<div>
<pre><span>library</span><span>(tidyverse)</span><br></pre>
</div>
<div>
<pre><span>myposts</span><span>$</span><span>author</span> <span>&lt;-</span> <span>word</span><span>(</span><span>string</span> <span>= myposts</span><span>$</span><span>title,</span> <span>start</span> <span>=</span> <span>1</span><span>,</span> <span>end</span> <span>=</span> <span>2</span><span>,</span> <span>sep</span> <span>=</span> <span>fixed</span><span>(</span><span>&quot; &quot;</span><span>))</span><br></pre>
</div>
</div>
</div>
<p>Finally, I was interested in extracting URLs I shared (mostly from YouTube or my own blog) and the text of my posts, which I did with some regular expression functions and some help from Stack Overflow (<a href="https://stackoverflow.com/questions/26496538/extract-urls-with-regex-into-a-new-data-frame-column">here</a> and <a href="https://stackoverflow.com/questions/21080967/how-to-get-the-first-10-words-in-a-string-in-r">here</a>).</p>
<div>
<div>
<div>
<pre><span>url_pattern</span> <span>&lt;-</span> <span>&quot;http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&amp;+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+&quot;</span><br><br><span>myposts</span><span>$</span><span>links</span> <span>&lt;-</span> <span>str_extract</span><span>(myposts</span><span>$</span><span>attachments, url_pattern)</span><br><br><span>library</span><span>(qdapRegex)</span><br></pre>
</div>
<div>
<pre><span>myposts</span><span>$</span><span>posttext</span> <span>&lt;-</span> <span>myposts</span><span>$</span><span>data</span> <span>%>%</span><br>  <span>rm_between</span><span>(</span><span>'&quot;'</span><span>,</span><span>'&quot;'</span><span>,</span><span>extract</span> <span>=</span> <span>TRUE</span><span>)</span><br></pre>
</div>
</div>
</div>
<p>There’s more cleaning I could do, but this gets me a data frame I could use for some text analysis. Let’s look at my most frequent words.</p>
<div>
<div>
<div>
<pre><span>myposts</span><span>$</span><span>posttext</span> <span>&lt;-</span> <span>as.character</span><span>(myposts</span><span>$</span><span>posttext)</span><br><span>library</span><span>(tidytext)</span><br><span>mypost_text</span> <span>&lt;-</span> <span>myposts</span> <span>%>%</span><br>  <span>unnest_tokens</span><span>(word, posttext)</span> <span>%>%</span><br>  <span>anti_join</span><span>(stop_words)</span><br></pre>
</div>
<div>
<pre>## Joining, by = &quot;word&quot;<br></pre>
</div>
<div>
<pre><span>counts</span> <span>&lt;-</span> <span>mypost_text</span> <span>%>%</span><br>  <span>filter</span><span>(author</span> <span>==</span> <span>&quot;Sara Locatelli&quot;</span><span>)</span> <span>%>%</span><br>  <span>drop_na</span><span>(word)</span> <span>%>%</span><br>  <span>count</span><span>(word,</span> <span>sort</span> <span>=</span> <span>TRUE</span><span>)</span><br><br><span>counts</span><br></pre>
</div>
<div>
<pre>## # A tibble: 9,753 x 2<br>##    word         n<br>##    </pre></div></div></div></div>