diff --git a/Assignment 2-2020.Rmd b/Assignment 2-2020.Rmd index 081fcec..2235d7f 100644 --- a/Assignment 2-2020.Rmd +++ b/Assignment 2-2020.Rmd @@ -1,7 +1,7 @@ --- title: "Assignment 2" -author: "Charles Lang" -date: "September 24, 2020" +author: "XI Gu" +date: "Oct 5, 2020" output: html_document --- #Part I @@ -19,6 +19,8 @@ key,points = how many times a student skipped or increased the speed of a video ```{r} #Install the 'tidyverse' package or if that does not work, install the 'dplyr' and 'tidyr' packages. + + #Load the package(s) you just installed library(tidyverse) @@ -26,9 +28,10 @@ library(tidyr) library(dplyr) D1 <- read.csv("video-data.csv", header = TRUE) - +D1 #Create a data frame that only contains the years 2018 D2 <- filter(D1, year == 2018) +D2 ``` ## Histograms @@ -63,7 +66,7 @@ y <- c(2,4,2,3,2,4,3) #Create a table from x & y table1 <- table(x,y) - +table1 #Display the table as a Barplot barplot(table1) @@ -75,6 +78,7 @@ plot(D3$year, D3$mean_key, type = "l", lty = "dashed") #Create a boxplot of total enrollment for three students D4 <- filter(D1, stid == 4|stid == 20| stid == 22) +D4 #The drop levels command will remove all the schools from the variable with no data D4 <- droplevels(D4) boxplot(D4$watch.time~D4$stid, xlab = "Student", ylab = "Watch Time") @@ -83,18 +87,28 @@ boxplot(D4$watch.time~D4$stid, xlab = "Student", ylab = "Watch Time") ```{r} #Use matrix notation to select columns 2, 5, 6, and 7 D5 <- D1[,c(2,5,6,7)] +D5 #Draw a matrix of plots for every combination of variables pairs(D5) ``` ## Part II 1. Create a simulated data set containing 100 students, each with a score from 1-100 representing performance in an educational game. The scores should tend to cluster around 75. Also, each student should be given a classification that reflects one of four interest groups: sport, music, nature, literature. - ```{r} #rnorm(100, 75, 15) creates a random sample with a mean of 75 and standard deviation of 20 +score <- rnorm(100, 75, 20) #filter() can be used to set a maximum and minimum value +S1 <- data.frame(score) +S1 <- filter(S1,score <= 100) #round() rounds numbers to whole number values +round(S1,digits=0) #sample() draws a random samples from the groups vector according to a uniform distribution +S2 <- data.frame(rep(100,100-nrow(S1))) +names(S2) <- "score" +S3 <- bind_rows(S1,S2) +interest<-c("sport","music","nuture","literature") +S3$interest<-sample(interest,100,replace=T) +S3$stid<-seq(1,100,1) ``` @@ -102,7 +116,7 @@ pairs(D5) 2. Using base R commands, draw a histogram of the scores. Change the breaks in your histogram until you think they best represent your data. ```{r} - +hist(S3$score,breaks=20) ``` @@ -110,7 +124,9 @@ pairs(D5) ```{r} #cut() divides the range of scores into intervals and codes the values in scores according to which interval they fall. We use a vector called `letters` as the labels, `letters` is a vector made up of the letters of the alphabet. - +label <- letters[1:10] +S3$breaks <- cut(S3$score,breaks= 10,labels = label) +S3 ``` 4. Now using the colorbrewer package (RColorBrewer; http://colorbrewer2.org/#type=sequential&scheme=BuGn&n=3) design a pallette and assign it to the groups in your data on the histogram. @@ -118,12 +134,12 @@ pairs(D5) ```{r} library(RColorBrewer) #Let's look at the available palettes in RColorBrewer - +display.brewer.all() #The top section of palettes are sequential, the middle section are qualitative, and the lower section are diverging. #Make RColorBrewer palette available to R and assign to your bins - +S3$colors <- brewer.pal(10,"Set3") #Use named palette in histogram - +hist(S3$score,col=S3$colors) ``` @@ -131,35 +147,38 @@ library(RColorBrewer) ```{r} #Make a vector of the colors from RColorBrewer +interest.col <- brewer.pal(4,"Dark2") +boxplot(score ~ interest, S3, col = interest.col) ``` 6. Now simulate a new variable that describes the number of logins that students made to the educational game. They should vary from 1-25. ```{r} - +S3$login<-sample(1:25,100,replace=T) ``` 7. Plot the relationships between logins and scores. Give the plot a title and color the dots according to interest group. ```{r} - - +plot(S3$login,S3$score,col=S3$colors,main="Student Logins vs. Scores") +S3$col1<-ifelse(S3$interest=="music","red","green") ``` 8. R contains several inbuilt data sets, one of these in called AirPassengers. Plot a line graph of the the airline passengers over time using this data set. ```{r} - +AP<- data.frame(AirPassengers) +plot(AirPassengers) ``` 9. Using another inbuilt data set, iris, plot the relationships between all of the variables in the data set. Which of these relationships is it appropraiet to run a correlation on? ```{r} - +pairs(iris) ``` # Part III - Analyzing Swirl @@ -201,3 +220,27 @@ The variables are: 10. **Extra credit** Convert the `datetime` variable into month-day-year format and create a new data frame (`DF6`) that shows the average correct for each day Finally use the knitr function to generate an html document from your work. Commit, Push and Pull Request your work back to the main branch of the repository. Make sure you include both the .Rmd file and the .html file. +```{r} +#1. Insert a new code block +#2. Create a data frame from the `swirl-data.csv` file called `DF1` +DF1 <- read.csv("swirl-data.csv", TRUE) + +#3.Create a new data frame that only includes the variables `hash`, `lesson_name` and `attempt` called `DF2` +DF2 <- data.frame(DF1[,c("hash","lesson_name","attempt")]) + +#4.Use the `group_by` function to create a data frame that sums all the attempts for each `hash` by each `lesson_name` called `DF3` +DF3 <- DF2 %>% group_by(hash,lesson_name) %>% summarise(attempt_sum = sum(attempt)) + +#5.On a scrap piece of paper draw what you think `DF3` would look like if all the lesson names were column names +#6. Convert `DF3` to this format +spread(DF3, lesson_name, attempt_sum) +#7. Create a new data frame from `DF1` called `DF4` that only includes the variables `hash`, `lesson_name` and `correct` +DF4 <- data_frame(DF1 [,c ("hash", "lesson_name", "correct")]) +#8. Convert the `correct` variable so that `TRUE` is coded as the **number** `1` and `FALSE` is coded as `0` +DF4$correct <- ifelse(DF4$correct == TRUE, 1, 0) +#9. Create a new data frame called `DF5` that provides a mean score for each student on each course +DF5 <- DF4 %>% group_by(hash, lesson_name) %>% summarise(mean_correct = mean(correct)) +#10. **Extra credit** Convert the `datetime` variable into month-day-year format and create a new data frame (`DF6`) that shows the average correct for each day + + +``` \ No newline at end of file