Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 76 additions & 3 deletions Assignment 3.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ library(igraph)

#First we will make an object that contains the graph information using our two dataframes EDGE and VERTEX. Notice that we have made "directed = TRUE" - our graph is directed since comments are being given from one student to another.

g <- graph.data.frame(EDGE, directed=TRUE, vertices=VERTEX)
g <- graph.data.frame(EDGE, directed=TRUE, vertices=VERTEX, vertex.color=VERTEX$gender, vertex.size=3)

#Now we can plot our graph using the force directed graphing technique - our old friend Fruchertman-Reingold!

plot(g,layout=layout.fruchterman.reingold)
plot(g,layout=layout.fruchterman.reingold, vertex.color=VERTEX$gender, vertex.size=3)

#There are many ways to change the attributes of the graph to represent different characteristics of the newtork. For example, we can color the nodes according to gender.

plot(g,layout=layout.fruchterman.reingold, vertex.color=VERTEX$gender)
plot(g,layout=layout.fruchterman.reingold, vertex.color=VERTEX$gender, vertex.size=2)

#We can change the thickness of the edge according to the number of times a particular student has sent another student a comment.

Expand Down Expand Up @@ -121,3 +121,76 @@ Once you have done this, also [look up](http://igraph.org/r/) how to generate th
### To Submit Your Assignment

Please submit your assignment by first "knitting" your RMarkdown document into an html file and then comit, push and pull request both the RMarkdown file and the html file.

```{r}
library(tidyr)
library(dplyr)
library(stringr)
library(igraph)

#input
C1 <- read.csv("hudk4050-classes.csv", stringAsFactors = FALSE, header = TRUE)
#copy
C2 <- C1

# Data Tidying
```{r}
#Make header first row
colnames(C2) <- C2[1,]
#Remove unwanted rows
C2 <- slice(C2,3:49)
#Remove last column
C2 <- select(C2, 1:8)
#Merge name columns
C2 <- unite(C2, "name", `First Name`, `Last Name`, sep = " ")
#Remove unpredictable characters from names
C2$name <- str_to_title(C2$name)
#Make all class letters capitals
C2 <- C2 %>% mutate_at(2:7, list(toupper))
#Remove whitespace between letter and numbers in class
C2 <- C2 %>% mutate_at(2:7, str_replace_all, " ", "")

#Restructuring
```{r}
#Dataframe with two variables
CS <- C2 %>% gather(label, class, 2:7, na.rm = TRUE, convert = FALSE) %>%
select(name, class)
#create new variable
C3 <- filter(C3. class !="")
#remove duplicate
C3 <- unique(C3)
#Spread 1s across classes
C3 <- spread(C3, class, count)
#make row names student names
rownames(C3) <- C3$name
#remove names column and hudk4050
C3 <- select(C3, -name, -HUDK4050)
#shortest
C3[is.na(C3)]<-0
#Matrix
```{r}
#matrix conversion
C4 <- as.matric(C3)
#person-person matrix
C4 <- C4 %*% t(C4)

```

#Graphing
```{r}
g <- graph.adjacency(C4, mode="undirected", diag = FALSE)
plot(g, layout=layout.fruchterman.reingold,
vertex.size - 4,
#degree(g)*0.9,
vertex.label.cex=0.8,
vertex.label.color="blue",
vertex.color="gainsboro"
```
#Centrality
```{r}
#Calculate the degree of centrality
sort(degree(g). decreasing = TRUE)
#calculate betweeness
sort(betweenness(g), decreasing = TRUE)