Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assignment 2 #234

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
108 changes: 100 additions & 8 deletions Assignment 2-2020.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,37 @@ pairs(D5)

```{r}
#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
#round rounds numbers to whole number values
#sample draws a random samples from the groups vector according to a uniform distribution
score <- rnorm(100, 75, 15)
hist(score,breaks = 30)
S1 <- data.frame(score)

library(dplyr)

S1 <- filter(S1,score <= 100)

S2 <- data.frame(rep(100, 100 - nrow(S1)))

names(S2) <- "score"

S3 <-bind_rows(S1,S2)

S3$score <- round(S3$score,0)

interest <- c("sport","music","nature","literature")

S3$interest <- sample(interest,100,replace= T)

S3$stid <- seq(1,100,1)

```

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= 10,xlim = c(40,100),ylim = c(0,20))


```

Expand All @@ -110,6 +131,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)


```

Expand All @@ -118,47 +142,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

#brewer.pal(n, name)
#n:Number of different colors in the palette, minimum 3, maximum depending on #palette
S3$colors <- brewer.pal(10,"Set3")
#Use named palette in histogram

hist(S3$score, col = S3$colors)
```


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,"Dark2")
boxplot(S3$score~S3$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(c(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}


S3$col1 <- ifelse(S3$interest == "music", "red", ifelse(S3$interest == "literature","green", ifelse(S3$interest == "sport","blue","yellow")))
plot(S3$login, S3$score, type = "p",col=S3$col1, 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}
AirPassengers
plot(AirPassengers, type = "l")


```


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}
plot(iris)

```

Expand All @@ -171,7 +204,17 @@ In this repository you will find data describing Swirl activity from the class s
### Instructions

1. Insert a new code block
```{r}

```

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:

Expand All @@ -185,19 +228,68 @@ 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 <- DF1 %>% select(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 <- DF2 %>% group_by(hash, lesson_name) %>% summarise(attempt_sum = sum(attempt ,na.rm = T))

```


5. On a scrap piece of paper draw what you think `DF3` would look like if all the lesson names were column names
```{r}

unique_lesson_name <- unique(DF3 $lesson_name)
unique_lesson_name
```


6. Convert `DF3` to this format
```{r}
library(reshape2)
DF31 <- DF3
dcast(DF31,DF31$hash ~ DF31$lesson_name)
DF31 <- na.omit(DF31)
DF3 <- DF31
```


7. Create a new data frame from `DF1` called `DF4` that only includes the variables `hash`, `lesson_name` and `correct`
```{r}
DF4 <- DF1 %>% select(hash, lesson_name,correct)

```


8. Convert the `correct` variable so that `TRUE` is coded as the **number** `1` and `FALSE` is coded as `0`
```{r}

boolean <- ifelse(DF4$correct == "TRUE", 1, 0)
DF4$correct <- boolean

```


9. Create a new data frame called `DF5` that provides a mean score for each student on each course
```{r}
#`hash`, `lesson_name` and `correct`
DF5 <- DF4 %>% group_by(hash, lesson_name) %>% summarise(mean_correct = mean(correct ,na.rm = T))
DF5 <- na.omit(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}
POSIXct_date <- as.POSIXct(DF1$datetime, origin="1970-01-01",tz="Pacific/Auckland")
POSIXct_date <- as.Date(POSIXct_date,"%m/%d/%Y",tz="Pacific/Auckland")
```

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.
1,061 changes: 1,061 additions & 0 deletions Assignment-2-2020.html

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# Assignment 2
### Data Wrangling and Visualization

In Assignment 2 we will be looking at some interaction data from students commenting on a class video. The file "comment-data.csv" shows which student responded to which student in an online video platform.
In Assignment 2 we will be practicing data manipulation including use of the tidyverse.

The instructions to Assignment 2 are in the Assignment 2.rmd file. Assignments are structured in three parts, in the first part you can just follow along with the code, in the second part you will need to apply the code and in the third part is completely freestyle, apply your new knowledge in a new way.
The instructions to Assignment 2 are in the Assignment 2-2020.rmd file. Assignments are structured in three parts, in the first part you can just follow along with the code, in the second part you will need to apply the code, and in the third part is completely freestyle and you are expected to apply your new knowledge in a new way.

**Please complete as much as you can by 5:00pm, 10/05/20**
**Please complete as much as you can by midnight EDT, 10/05/20**

Once you have finished, commit, push and pull your assignment back to the main branch. Include the .Rmd file and the .html file generated from your .Rmd file.
Once you have finished, commit, push and pull your assignment back to the main branch. Include both the .Rmd file and the .html file.

Good luck!
13 changes: 13 additions & 0 deletions assignment2.Rproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Version: 1.0

RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: pdfLaTeX