From e006e4f9759b93e910b2e6267e28df510c312ca9 Mon Sep 17 00:00:00 2001 From: XiGu0313 <64285369+XiGu0313@users.noreply.github.com> Date: Thu, 22 Oct 2020 00:17:04 -0400 Subject: [PATCH] Assignment #3_XI GU --- Assignment 3.Rmd | 99 ++++++- Assignment-3.html | 665 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 763 insertions(+), 1 deletion(-) create mode 100644 Assignment-3.html diff --git a/Assignment 3.Rmd b/Assignment 3.Rmd index 649407e..f1e5dbc 100644 --- a/Assignment 3.Rmd +++ b/Assignment 3.Rmd @@ -96,7 +96,7 @@ plot(g,layout=layout.fruchterman.reingold, vertex.color=VERTEX$gender) plot(g,layout=layout.fruchterman.reingold, vertex.color=VERTEX$gender, edge.width=EDGE$count) -```` +``` ## Part II @@ -105,7 +105,19 @@ In Part II your task is to [look up](http://igraph.org/r/) in the igraph documen * Ensure that sizing allows for an unobstructed view of the network features (For example, the arrow size is smaller) * The vertices are colored according to major * The vertices are sized according to the number of comments they have recieved +```{r} +##Q1 +plot(g,layout=layout.fruchterman.reingold, vertex.color=VERTEX$major,vertex.size=15,edge.arrow.size=0.5) +##Q2 +plot(g,layout=layout.fruchterman.reingold, vertex.color=VERTEX$major,edge.width=EDGE$count,vertex.size=15,edge.arrow.size=0.5) +##Q3 +num1 <- count(D2, comment.to) +names(num1) <- c("id","count") +num1 <- left_join(VERTEX,num1,by=c("id")) +num1$count[is.na(num1$count)] <- 0 +plot(g,layout=layout.fruchterman.reingold, vertex.color=VERTEX$major, edge.width=EDGE$count, vertex.size=15+num1$count, edge.arrow.size=0.5) +``` ## Part III Now practice with data from our class. This data is real class data directly exported from Qualtrics and you will need to wrangle it into shape before you can work with it. Import it into R as a data frame and look at it carefully to identify problems. @@ -121,3 +133,88 @@ 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 data +C1 <- read.csv("hudk4050-classes.csv", stringsAsFactors = FALSE, header= TRUE) + +#Copy to play with data +C2 <- C1 + +``` +#Daa Tidying +```{r} +#Make header first row +colnames(C2) <- C2[1,] +#Remove unwated 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_replace(C2$name, "`", "") +#Make all name captalized first letters only +C2$name <- str_to_title(C2$name) +#Make all class letters capitals +C2 <- C2 %>% mutate_at(2:7, list(toupper)) +#Remove whitespace between letters and numbers in class +C2 <- C2 %>% mutate_at(2:7, str_replace_all, " ", "") + +``` + +# Data restructuring +```{r} +# Create a dataframe with two variables, student and class +C3 <- C2 %>%gather(labe, class, 2:7, na.rm = TRUE, convert = FALSE) %>% select(name, class) +#Create a new variable containing 1s that will become our counts +C3$count <- 1 +#Remove blank classes +C3 <-filter(C3, class != "") +#Remove duplicates (Danny!) +C3 <- unique(C3) +#Spread 1s across classes to create a student x class dataframe +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 +#Cheat way +C3 <-ifelse(is.na(C3),0,1) + +``` + +#Matrix operations +```{r} +#Convert to matrix +C4 <- as.matrix(C3) +#Create 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, + vertex.label.cex =0.8, + vertex.label.color="black", + vertex.color="yellow") + +``` + +#Centrality +```{r} +sort(degree(g), decreasing = TRUE) + +sort(betweenness(g), decreasing = TRUE) +``` diff --git a/Assignment-3.html b/Assignment-3.html new file mode 100644 index 0000000..e103e40 --- /dev/null +++ b/Assignment-3.html @@ -0,0 +1,665 @@ + + + + +
+ + + + + + + + +# Create a dataframe with two variables, student and class
+C3 <- C2 %>%gather(labe, class, 2:7, na.rm = TRUE, convert = FALSE) %>% select(name, class)
+#Create a new variable containing 1s that will become our counts
+C3$count <- 1
+#Remove blank classes
+C3 <-filter(C3, class != "")
+#Remove duplicates (Danny!)
+C3 <- unique(C3)
+#Spread 1s across classes to create a student x class dataframe
+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
+#Cheat way
+C3 <-ifelse(is.na(C3),0,1)
+#Matrix operations
+#Convert to matrix
+C4 <- as.matrix(C3)
+#Create person-person matrix
+C4 <- C4 %*% t(C4)
+#Graphing
+g <- graph.adjacency(C4, mode="undirected", diag = FALSE)
+
+plot(g,layout=layout.fruchterman.reingold,
+ vertex.size = 4,
+ vertex.label.cex =0.8,
+ vertex.label.color="black",
+ vertex.color="yellow")
+#Centrality
+sort(degree(g), decreasing = TRUE)
+## Abdul Malik Muftau Ali Al Jabri Amandaoliveira Berjakian
+## 2346 2346 2346 2346
+## Chriskim Danlei Dannyshan Fangqiliu
+## 2346 2346 2346 2346
+## Feiwang Guoliangxu Hangshijin He Chen
+## 2346 2346 2346 2346
+## Hyungoolee Jiaao Qi Jiacongzhu Jiahaoshen
+## 2346 2346 2346 2346
+## Jiashengyu Jieyao Kaijie Fang Mahshaddavoodifard
+## 2346 2346 2346 2346
+## Nicoleschlosberg Qianhuiyuan Rongsang Ruoyi Zhang
+## 2346 2346 2346 2346
+## Saravasquez Shuyingxiong Stanley Si Hengzhao Tianyuchang
+## 2346 2346 2346 2346
+## Vidyamadhavan Wenningxiao Wenqigao Xiaojialiu
+## 2346 2346 2346 2346
+## Xijiawang Xiyun Zhang Xueshiwang Yifeizhang
+## 2346 2346 2346 2346
+## Yingxinxie Yingxinye Yixiongxu Yuchengpan
+## 2346 2346 2346 2346
+## Yunzhaowu Yuruiwang Yutingzhou Yuxuange
+## 2346 2346 2346 2346
+## Zachfriedman Zhixin Zheng Zhoudawu
+## 2346 2346 2346
+sort(betweenness(g), decreasing = TRUE)
+## Abdul Malik Muftau Ali Al Jabri Amandaoliveira Berjakian
+## 0 0 0 0
+## Chriskim Danlei Dannyshan Fangqiliu
+## 0 0 0 0
+## Feiwang Guoliangxu Hangshijin He Chen
+## 0 0 0 0
+## Hyungoolee Jiaao Qi Jiacongzhu Jiahaoshen
+## 0 0 0 0
+## Jiashengyu Jieyao Kaijie Fang Mahshaddavoodifard
+## 0 0 0 0
+## Nicoleschlosberg Qianhuiyuan Rongsang Ruoyi Zhang
+## 0 0 0 0
+## Saravasquez Shuyingxiong Stanley Si Hengzhao Tianyuchang
+## 0 0 0 0
+## Vidyamadhavan Wenningxiao Wenqigao Xiaojialiu
+## 0 0 0 0
+## Xijiawang Xiyun Zhang Xueshiwang Yifeizhang
+## 0 0 0 0
+## Yingxinxie Yingxinye Yixiongxu Yuchengpan
+## 0 0 0 0
+## Yunzhaowu Yuruiwang Yutingzhou Yuxuange
+## 0 0 0 0
+## Zachfriedman Zhixin Zheng Zhoudawu
+## 0 0 0
+