Skip to content
Open
Show file tree
Hide file tree
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
67 changes: 57 additions & 10 deletions Assignment 2-2020.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ D1 <- read.csv("video-data.csv", header = TRUE)

#Create a data frame that only contains the years 2018
D2 <- filter(D1, year == 2018)
#preview the tibble. I like to get a preview of the data this way instead of clicking on the table or using the View command
D2
```

## Histograms
Expand Down Expand Up @@ -90,18 +92,24 @@ pairs(D5)

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}
```{r simulate data,}
#rnorm(100, 75, 15) creates a random sample with a mean of 75 and standard deviation of 20
#pmax sets a maximum value, pmin sets a minimum value
#round rounds numbers to whole number values
#sample draws a random samples from the groups vector according to a uniform distribution
#I used expand_grid to stay in the tidyverse

stid=seq(1,100,1)
scores=round(pmin(100,pmax(1, rnorm(100,75,15))))
interest=sample(c("sport","music","nature","literature"), 100, replace=TRUE)

EG <- tibble(stid,scores,interest)
```

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(EG$scores, breaks =7,xlab = "Scores", main = "Educational Game - Score Distribution")


```

Expand All @@ -110,6 +118,8 @@ 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:7]
EG$breaks<- cut(EG$scores, breaks =7, labels = label)

```

Expand All @@ -118,48 +128,56 @@ 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

#Use named palette in histogram

hist(EG$scores, breaks = 7, col = brewer.pal(6,"Blues"))
```


5. Create a boxplot that visualizes the scores for each interest group and color each interest group a different color.

```{r}
#Make a vector of the colors from RColorBrewer

interest.col=brewer.pal(4,"Pastel2")
boxplot(EG$scores~EG$interest, col=interest.col, xlab = "Student Interest", ylab = "Scores")
```


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}
EG$logins=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(EG$logins, EG$scores, col=interest.col, xlab = "Number of Log-ins", ylab = "Scores", main = "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}
data("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?
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 appropriate to run a correlation on?

```{r}

data("iris")
pairs(iris)
plot(iris$Petal.Length, iris$Petal.Width, main = "Relationship between Petal Lenght and petal width")
cor(iris$Petal.Length, iris$Petal.Widt)
```

# Part III - Analyzing Swirl
Expand All @@ -171,7 +189,13 @@ In this repository you will find data describing Swirl activity from the class s
### Instructions

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 = T)

```


The variables are:

Expand All @@ -185,19 +209,42 @@ The variables are:
`hash` - anonymyzed student ID

3. Create a new data frame that only includes the variables `hash`, `lesson_name` and `attempt` called `DF2`
```{r}
DF2<-select(DF1, c(8,2,5))
```

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`

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`
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
```{r}
DF3 <- DF2 %>% group_by(hash,lesson_name) %>% summarise(attempts=sum(attempt), .groups='drop') %>% drop_na(attempts) %>%pivot_wider(names_from = lesson_name, values_from=attempts)
```


7. Create a new data frame from `DF1` called `DF4` that only includes the variables `hash`, `lesson_name` and `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 <- select(DF1, c(hash,lesson_name,correct)) %>%mutate(correct= recode(correct, "TRUE"=1, "FALSE"=0))
```


9. Create a new data frame called `DF5` that provides a mean score for each student on each course
```{r}
DF5 <-DF4 %>% group_by(hash,lesson_name) %>% drop_na(correct)%>% summarise(mean_correct=mean(correct, na.rm = TRUE), .groups='drop') %>% pivot_wider(names_from = lesson_name, values_from=mean_correct)
DF5
```


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 <- select(DF1,correct,datetime)
DF6$correct <- ifelse(DF6$correct== TRUE, 1,0)
DF6$datetime <- as.POSIXlt(DF6$datetime, origin="1970-01-01 00:00.00 UTC")
DF6$datetime <- strftime(DF6$datetime, format = "%b:%e")
DF7 <- DF6 %>% group_by(datetime) %>% summarise(av_correct=mean(correct,na.rm = TRUE))
```


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.
Loading