---
title: 'SOCI424/624: Getting familiar with R (Markdown)'
author: "Peter McMahan"
output: html_document
---

```{r setup, include=FALSE}
# This is a section you can use to run R commands
# that you *don't* want to show up in your final
# document. You can ignore it for now.
knitr::opts_chunk$set(echo = TRUE)
```

# R Markdown

This is an R Markdown document. R Markdown is a popular format for writing documents that mix text (written in Markdown) and computer code (written in R). 

Markdown is a simple format that allows you to write in "plain text" (text that doesn't have any formatting) but to represent common text formatting like **bold**, _italics_, headers (see the '#' above), and even [links](https://plain-text.co/). Once you have a markdown file it is simple to turn it into a PDF, an HTML document, or a MS Word document.

What makes R _Markdown_ different than regular Markdown is that it will evaluate R commands that you write in your document.

This document is set up to render as HTML. If you're reading this file in RStudio, you can view the HTML document by clicking the **knit** button at the top of this pane.

You can embed simple R commands right in your text (e.g. simple math like `r 2+2`), or, more frequently, you can embed more complex "code chunks":

```{r cars}
# Print a summary of the `cars` dataset that comes built in to R.
# This data contains 50 observations of the stopping distance of 
# cars at different speeds
summary(cars)
```

## Including Plots

You can also embed plots, for example:

```{r cars_plot, echo=FALSE}
plot(cars$speed, cars$dist, xlab="speed (MPH)", ylab="distance (ft)")
```

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.


## installing `igraph`

Although much of what you will work on below will use R's base functionality, we will also introduce a network-specific add-on package called `igraph`. This package enables a host of network-analytic methods and visualizations to R. But to use `igraph` you will first need to install it (if you haven't already) using one of the following methods:

-   Use "Tools \> Install Packages ..." in RStudio, and type "`igraph`"" into the search bar

**OR**

-   Type `install.packages('igraph')` into the R console

Once you've done that, the following code chunk will load `igraph` and get it ready to use below. (Note: this will bring up a few warnings about "masked objects" which can be safely ignored.)

```{r packages}
library(igraph)
```

# Now you!

In the code chunk below, try loading the character interaction data from Shakespear's _A Midsummer Night's Dream_ (available directlhy from <https://soci424.netlify.app/data/AMND_characters.csv>) into a variable called `midsummer` and printing out a summary of the resulting data. (Note: `read.csv()` can take a url as it's argument, and `summary()` will print a quick summary of a data frame.)

```{r midsummer_load}
# your code here
```

Now, use R to calculate how many rows and how many columns there are in this data frame (the `nrow()` and `ncol()` functions will do the trick):

```{r midsummer_shape}
# your code here
```

Use the `head()` function to print the first _15_ rows of the data frame (by default it only gives you _6_ rows):

```{r midsummer_head}
# your code here
```

How many unique names are listed in the first _two_ columns of the data frame?

```{r midsummer_uniquenames}
# your code here
```

How many rows in the data frame represent only one interaction?

```{r midsummer_singleinteraction}
# your code here
```

Which pair(s) of names have the highest number of interactions?

```{r midsummer_largestweight}
# your code here
```


Looking at all of your output above, talk about what specific aspect of interaction you think this data might represent. What does a single row tell us?

_**Bonus:**_ Does this data represent _directed_ (asymmetric) or _undirected_ (symmetric) relations? You can figure this out by considering the order of the names in each row.


## Creating a network representation

One benefit of `igraph` is that it can read data in many different formats and represent them as a unified "`igraph`" class. Use igraph's `graph_from_data_frame()` function to create an `igraph` object from the data frame you've already loaded.

```{r midsummer_makenetwork}
# your code here
```

Use the `degree()` function on your new network object. Comparing this output to the data frame, what does it seem to represent? What if you specify the `mode` argument as either `'out'` or `'in'`?

```{r midsummer_degree}
# your code here
```

Finally, create a simple visualization of the network. We'll get into details about this later, but for the moment you can simply use `plot()`.

```{r midsummer_plot}
# your code here
```

