diff --git a/Assignment 2-2020.Rmd b/Assignment 2-2020.Rmd index 081fcec..48db062 100644 --- a/Assignment 2-2020.Rmd +++ b/Assignment 2-2020.Rmd @@ -95,13 +95,30 @@ pairs(D5) #filter() can be used to set a maximum and minimum value #round() rounds numbers to whole number values #sample() draws a random samples from the groups vector according to a uniform distribution +``` +```{r} +rnorm(100,75,15) +``` +```{r} + +score <- rnorm(100,75,15) +s1 <- data.frame(score) +s1 <- filter(s1, score <= 100) +s2 <- data.frame(rep(100, 100 - nrow(s1))) +names(s2) <- "score" +interest <- c("sport", "music", "nature", "literature") + ``` 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(score, breaks = 10) +``` +```{r} + ``` @@ -113,6 +130,16 @@ pairs(D5) ``` +```{r} +s3 <- bind_rows(s1,s2) +s3$score <- ifelse(s3$score > 100,100, s3$score ) +s3$score <- round(s3$score,0) +s3$interest <- sample(interest, 100, replace = TRUE) +s3$stid <- seq(1,100,1) +label <- letters[1:10] +s3$breaks <- cut(s3$score, breaks = 10, labels = label) +``` + 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. ```{r} @@ -124,6 +151,20 @@ library(RColorBrewer) #Use named palette in histogram +``` +```{r} +display.brewer.all() +``` + + +```{r} +s3$colors <- brewer.pal(10, "Set3") +``` + + +```{r} +hist(s3$score, col = s3$colors) + ``` @@ -132,34 +173,46 @@ library(RColorBrewer) ```{r} #Make a vector of the colors from RColorBrewer +interest.col <- brewer.pal(4, "Dark2") +``` + + +```{r} +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 = TRUE) ``` 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") ``` 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(AP) ``` 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} +i <- data.frame(iris) +plot(i) +``` +```{r} +pairs(iris) ``` # Part III - Analyzing Swirl @@ -172,6 +225,9 @@ In this repository you will find data describing Swirl activity from the class s 1. Insert a new code block 2. Create a data frame from the `swirl-data.csv` file called `DF1` +```{r} +DF1 <- read.csv("swirl-data.csv" , header = TRUE) +``` The variables are: @@ -184,20 +240,43 @@ The variables are: `datetime` - the date and time the student attempted the question `hash` - anonymyzed student ID -3. Create a new data frame that only includes the variables `hash`, `lesson_name` and `attempt` called `DF2` +3. Create a new data frame that only includes the variables `hash`, `lesson_name` and `attempt` called `DF2`DF3 +```{r} +DF2 <- data.frame(DF1$hash, DF1$lesson_name, DF1$attempt) +names(DF2) <- 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` +```{r} +DF3 <- DF1 %>% group_by(hash, lesson_name) %>% summarise(sum_attempt = 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 7. Create a new data frame from `DF1` called `DF4` that only includes the variables `hash`, `lesson_name` and `correct` +```{r} +DF4 <- data.frame(DF1$hash, DF1$lesson_name , DF1$correct) +names(DF4) <- 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` -8. Convert the `correct` variable so that `TRUE` is coded as the **number** `1` and `FALSE` is coded as `0` +```{r} +DF4[,"correct"] <- lapply(is.logical(DF4[,"correct"]), as.numeric) +``` 9. Create a new data frame called `DF5` that provides a mean score for each student on each course +```{r} + DF5 <- DF1 %>% group_by(hash, course_name) %>% summarise(mean_score = mean(score)) +``` 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 +```{r} +DF6 <- as.Date(as.Date(DF1$datetime, origin = "0000-01-01"), "%m%d%y") +``` 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. diff --git a/Assignment-2-2020.html b/Assignment-2-2020.html new file mode 100644 index 0000000..05a53d6 --- /dev/null +++ b/Assignment-2-2020.html @@ -0,0 +1,644 @@ + + + + +
+ + + + + + + + + + +#Part I
+In the hackathon a project was proposed to collect data from student video watching, a sample of this data is available in the file video-data.csv.
+stid = student id year = year student watched video participation = whether or not the student opened the video watch.time = how long the student watched the video for confusion.points = how many times a student rewatched a section of a video key,points = how many times a student skipped or increased the speed of a video
+#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)
+## ── Attaching packages ───────────────── tidyverse 1.3.0 ──
+## ✓ ggplot2 3.3.2 ✓ purrr 0.3.4
+## ✓ tibble 3.0.3 ✓ dplyr 1.0.2
+## ✓ tidyr 1.1.2 ✓ stringr 1.4.0
+## ✓ readr 1.3.1 ✓ forcats 0.5.0
+## ── Conflicts ──────────────────── tidyverse_conflicts() ──
+## x dplyr::filter() masks stats::filter()
+## x dplyr::lag() masks stats::lag()
+library(tidyr)
+library(dplyr)
+
+D1 <- read.csv("video-data.csv", header = TRUE)
+
+#Create a data frame that only contains the years 2018
+D2 <- filter(D1, year == 2018)
+#Generate a histogram of the watch time for the year 2018
+
+hist(D2$watch.time)
+#Change the number of breaks to 100, do you get the same impression?
+
+hist(D2$watch.time, breaks = 100)
+#Cut the y-axis off at 10
+
+hist(D2$watch.time, breaks = 100, ylim = c(0,10))
+#Restore the y-axis and change the breaks so that they are 0-5, 5-20, 20-25, 25-35
+
+hist(D2$watch.time, breaks = c(0,5,20,25,35))
+#Plot the number of confusion points against the watch time
+
+plot(D1$confusion.points, D1$watch.time)
+#Create two variables x & y
+x <- c(1,3,2,7,6,4,4)
+y <- c(2,4,2,3,2,4,3)
+
+#Create a table from x & y
+table1 <- table(x,y)
+
+#Display the table as a Barplot
+barplot(table1)
+#Create a data frame of the average total key points for each year and plot the two against each other as a lines
+
+D3 <- D1 %>% group_by(year) %>% summarise(mean_key = mean(key.points))
+## `summarise()` ungrouping output (override with `.groups` argument)
+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)
+#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")
+ ## Pairs
#Use matrix notation to select columns 2, 5, 6, and 7
+D5 <- D1[,c(2,5,6,7)]
+#Draw a matrix of plots for every combination of variables
+pairs(D5)
+ ## Part II
#rnorm(100, 75, 15) creates a random sample with a mean of 75 and standard deviation of 20
+#filter() can be used to set a maximum and minimum value
+#round() rounds numbers to whole number values
+#sample() draws a random samples from the groups vector according to a uniform distribution
+rnorm(100,75,15)
+## [1] 67.84873 79.80337 42.63363 85.17714 88.59926 78.46961 46.36595
+## [8] 76.35964 79.62964 82.98106 68.94288 50.53024 66.48003 75.96119
+## [15] 86.22544 84.49252 72.89383 65.95827 97.22493 54.58226 82.50733
+## [22] 94.73172 76.44061 48.69983 78.32315 83.59933 67.24537 61.78429
+## [29] 55.76038 99.00084 77.70035 80.31684 64.35895 74.29432 68.29162
+## [36] 83.00360 65.45195 119.01862 67.00178 74.73462 100.20655 77.12589
+## [43] 79.70799 74.96633 65.96527 99.73169 54.87964 66.05609 69.12125
+## [50] 63.00844 73.41591 66.77133 55.24943 42.70290 69.43047 87.32524
+## [57] 68.76847 90.57337 68.39631 70.40294 69.44273 73.48045 91.93331
+## [64] 84.09845 68.49322 95.99793 67.21138 91.16768 77.23583 55.79581
+## [71] 77.66248 91.68355 91.19191 76.58508 88.77551 59.62973 71.22696
+## [78] 72.67956 69.22533 86.21434 70.26697 87.91134 56.22038 67.29668
+## [85] 54.67929 56.39107 87.98281 69.60382 63.19514 102.80703 63.33112
+## [92] 67.86027 86.34639 76.84172 87.01969 83.83585 77.42888 48.61309
+## [99] 103.28562 87.18372
+score <- rnorm(100,75,15)
+s1 <- data.frame(score)
+s1 <- filter(s1, score <= 100)
+s2 <- data.frame(rep(100, 100 - nrow(s1)))
+names(s2) <- "score"
+interest <- c("sport", "music", "nature", "literature")
+hist(score, breaks = 10)
+#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.
+s3 <- bind_rows(s1,s2)
+s3$score <- ifelse(s3$score > 100,100, s3$score )
+s3$score <- round(s3$score,0)
+s3$interest <- sample(interest, 100, replace = TRUE)
+s3$stid <- seq(1,100,1)
+label <- letters[1:10]
+s3$breaks <- cut(s3$score, breaks = 10, labels = label)
+library(RColorBrewer)
+#Let's look at the available palettes in RColorBrewer
+
+#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
+
+#Use named palette in histogram
+display.brewer.all()
+s3$colors <- brewer.pal(10, "Set3")
+hist(s3$score, col = s3$colors)
+#Make a vector of the colors from RColorBrewer
+
+interest.col <- brewer.pal(4, "Dark2")
+boxplot(score ~ interest, s3, col = interest.col)
+s3$login <- sample(1:25, 100, replace = TRUE)
+plot(s3$login, s3$score, col = s3$colors, main = "Student logins vs. Scores")
+AP <- data.frame(AirPassengers)
+plot(AP)
+i <- data.frame(iris)
+plot(i)
+pairs(iris)
+In this repository you will find data describing Swirl activity from the class so far this semester. Please connect RStudio to this repository.
+swirl-data.csv file called DF1DF1 <- read.csv("swirl-data.csv" , header = TRUE)
+The variables are:
+course_name - the name of the R course the student attempted
+lesson_name - the lesson name
+question_number - the question number attempted correct - whether the question was answered correctly
+attempt - how many times the student attempted the question
+skipped - whether the student skipped the question
+datetime - the date and time the student attempted the question
+hash - anonymyzed student ID
hash, lesson_name and attempt called DF2DF3DF2 <- data.frame(DF1$hash, DF1$lesson_name, DF1$attempt)
+names(DF2) <- c("hash", "lesson_name", "attempt")
+group_by function to create a data frame that sums all the attempts for each hash by each lesson_name called DF3DF3 <- DF1 %>% group_by(hash, lesson_name) %>% summarise(sum_attempt = sum(attempt))
+## `summarise()` regrouping output by 'hash' (override with `.groups` argument)
+On a scrap piece of paper draw what you think DF3 would look like if all the lesson names were column names
Convert DF3 to this format
Create a new data frame from DF1 called DF4 that only includes the variables hash, lesson_name and correct
DF4 <- data.frame(DF1$hash, DF1$lesson_name , DF1$correct)
+names(DF4) <- c("hash", "lesson_name", "correct")
+correct variable so that TRUE is coded as the number 1 and FALSE is coded as 0DF4[,"correct"] <- lapply(is.logical(DF4[,"correct"]), as.numeric)
+DF5 that provides a mean score for each student on each course DF5 <- DF1 %>% group_by(hash, course_name) %>% summarise(mean_score = mean(score))
+## `summarise()` regrouping output by 'hash' (override with `.groups` argument)
+datetime variable into month-day-year format and create a new data frame (DF6) that shows the average correct for each dayDF6 <- as.Date(as.Date(DF1$datetime, origin = "0000-01-01"), "%m%d%y")
+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.
+