From 3ddd57f7504e879a3577248ad846ecd7ede78597 Mon Sep 17 00:00:00 2001 From: AJ Date: Wed, 4 Nov 2020 17:58:05 -0500 Subject: [PATCH] Assignment 3 Submission --- Assignment 3.Rmd | 79 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 76 insertions(+), 3 deletions(-) diff --git a/Assignment 3.Rmd b/Assignment 3.Rmd index 649407e..31afd74 100644 --- a/Assignment 3.Rmd +++ b/Assignment 3.Rmd @@ -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. @@ -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) + + \ No newline at end of file