{"componentChunkName":"component---src-templates-post-template-js","path":"/posts/importing-social-network-data-into-r","result":{"data":{"markdownRemark":{"id":"09146216-0101-5b0f-aba9-864ddc69b415","html":"<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">library(knitr)\nlibrary(printr)</code></pre></div>\n<p>In my professional life, I manage and analyze data on a team that studies the social networks surrounding children with autism. The purpose of this post is not to discuss that work in depth, but rather to show how to quickly and easily import one type of data I work with into R. For social network analysis, I use the package <a href=\"http://igraph.org/r/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">igraph</a>.</p>\n<p>The type of data I’m going to talk about importing today is egocentric network data. For those that don’t know, egocentric network data involves asking a single person about the makeup of an entire network.</p>\n<h2 id=\"collecting-egocentric-network-data\" style=\"position:relative;\"><a href=\"#collecting-egocentric-network-data\" aria-label=\"collecting egocentric network data permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Collecting Egocentric Network Data</h2>\n<p>To demonstrate, imagine that I’m interested in social networks at the gym and that you go to the gym with a three friends every week. I approach you and after asking you to join my study, ask you who you go the gym with.</p>\n<p>You say “James, Jen, and Rene.”</p>\n<p>Then I ask some questions about the group in relation to James:</p>\n<blockquote>\n<p>How many times a week do you see James?</p>\n</blockquote>\n<blockquote>\n<p>How many times a week does James see Jen?</p>\n</blockquote>\n<blockquote>\n<p>How many times a week does James see Rene?</p>\n</blockquote>\n<p>We then do the same for Jen and Rene where I ask you often you see Jen and how often she sees Rene, and then I ask how often you see Rene.<sup id=\"fnref-1\"><a href=\"#fn-1\" class=\"footnote-ref\">1</a></sup></p>\n<p>My survey here came in two parts:</p>\n<ol>\n<li>A <strong>name generatorr</strong> where I ask you who you interact with</li>\n<li>A <strong>name modifier</strong> where I ask you how you interact with each person</li>\n</ol>\n<p>After the data are collected, I log it all in my spreadsheet where each row corresponds to a data collection instance.</p>\n<p>So, I have data that look like this:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">library(dplyr)\n\ngym_networks &lt;- data_frame(\n  participant = c(&quot;You&quot;, &quot;Bart&quot;, &quot;Lisa&quot;),\n  p1 = c(&quot;James&quot;, &quot;Milhouse&quot;, &quot;Sherry&quot;),\n  p2 = c(&quot;Jen&quot;, &quot;Nelson&quot;, &quot;Terry&quot;),\n  p3 = c(&quot;Rene&quot;, &quot;Martin&quot;, &quot;Ralph&quot;),\n  participant_x_p1 = sample(1:10, 3),\n  p1_x_p2 = sample(1:10, 3),\n  p1_x_p3 = sample(1:10, 3),  \n  participant_x_p2 = sample(1:10, 3),\n  p2_x_p3 = sample(1:10, 3),\n  participant_x_p3 = sample(1:10, 3)\n)\nprint(gym_networks)</code></pre></div>\n<h2 id=\"getting-the-data-ready-for-analysis\" style=\"position:relative;\"><a href=\"#getting-the-data-ready-for-analysis\" aria-label=\"getting the data ready for analysis permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Getting the data ready for analysis</h2>\n<p>The data here are represented in an adjacency list with weights attached. Although igraph has a function for importing adjacency lists, it isn’t not configured to handle weights, so we will take our adjacency list and convert it into an edge list, which igraph can handle with weights.</p>\n<p>To accomplish this, we’ll use the package <a href=\"http://tidyr.tidyverse.org/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">tidyr</a>.</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">library(tidyr)\n\nel &lt;- gym_networks %&gt;%\n  # Step 1: Make each row a single edge\n  gather(key, value = &quot;weight&quot;, -(participant:p3)) %&gt;%\n  # Step 2: Configure two new columns, an ego, and an alter\n  mutate(ego = case_when(grepl(&quot;participant&quot;, key) ~ participant,\n                         grepl(&quot;p1_&quot;, key) ~ p1,\n                         grepl(&quot;p2_&quot;, key) ~ p2,\n                         grepl(&quot;p3_&quot;, key) ~ p3),\n         alter = case_when(grepl(&quot;_p1&quot;, key) ~ p1,\n                           grepl(&quot;_p2&quot;, key) ~ p2,\n                           grepl(&quot;_p3&quot;, key) ~ p3)) %&gt;%\n  # Step 3: Clean up the data frame\n  select(ego, alter, weight)\n\nprint(head(el))</code></pre></div>\n<p>In three steps, we go from adjacency list to edge list. In one more step, we have an igraph object to analyse and plot!</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">library(igraph)\n\ngraph &lt;- graph_from_data_frame(el)</code></pre></div>\n<p>Happy analyzing, friends!</p>\n<div class=\"footnotes\">\n<hr>\n<ol>\n<li id=\"fn-1\">\n<p>I don’t ask how often Jen sees James and how often Rene sees James or Jen because we assume that it takes two to tango in this respect.</p>\n<a href=\"#fnref-1\" class=\"footnote-backref\">↩</a>\n</li>\n</ol>\n</div>","fields":{"slug":"/posts/importing-social-network-data-into-r","tagSlugs":["/tag/r/","/tag/social-network-analysis/","/tag/igraph/","/tag/egocentric-networks/"]},"frontmatter":{"date":"2017-12-19","description":"In my professional life, I manage and analyze data on a team that studies the social networks surrounding children with autism. The purpose of this post is not to discuss that work in depth, but rather to show how to quickly and easily import one type of data I work with into R. ","tags":["r","Social Network Analysis","igraph","egocentric networks"],"title":"Importing Social Network Data into R","socialImage":null}}},"pageContext":{"slug":"/posts/importing-social-network-data-into-r"}}}