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

Class-activity 2 #9

Open
wants to merge 3 commits 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
30 changes: 22 additions & 8 deletions class-activity-2.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ D2 <- filter(D1, schoolyear == 20112012)
```{r}
#Generate a histogramof the percentage of free/reduced lunch students (frl_percent) at each school

hist()
hist(D2$frl_percent)

#Change the number of breaks to 100, do you get the same impression?

Expand Down Expand Up @@ -80,14 +80,19 @@ pairs(D5)
#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

scores <- round(pmax(1, pmin(100, rnorm(100, 75, 20))))
studentid <- seq(1, 100, 1)
groups <- c("sport", "music", "nature", "literature")
interest <- sample(groups, 100, replace = T)
D1 <- data.frame(studentid, 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(D1$scores, breaks = 100)
```


Expand All @@ -96,18 +101,21 @@ 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.

D1$binned = cut(D1$scores, breaks = 10, labels = letters[1:10])

```

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}
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

D1$colors <- brewer.pal(10, "Set3")
#Use named palette in histogram
hist(D1$score, col = D1$colors)

```

Expand All @@ -116,35 +124,41 @@ library(RColorBrewer)

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

interest.col <- brewer.pal(4, "Dark2")
boxplot(scores ~ interest, D1, col = interest.col)
plot(D1$interest, D1$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}
D1$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}

D1$col1 <- ifelse (D1$interest == "music", "red", "green")
plot(D1$login, D1$scores, col = D1$col1)

```


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)
head(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}

pairs(iris)
```

10. Finally use the knitr function to generate an html document from your work. If you have time, try to change some of the output using different commands from the RMarkdown cheat sheet.
Expand Down
13 changes: 13 additions & 0 deletions class-activity-2.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