-
Notifications
You must be signed in to change notification settings - Fork 0
/
HW1_Solutions.qmd
113 lines (75 loc) · 4.38 KB
/
HW1_Solutions.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
---
title: "Homework 1"
author: "The Big Picture & Introduction to R"
format: pdf
editor: visual
---
## Problem 1: Basic R Commands and Structures
For this problem, please load the `Virginis.interp` data set into your R environment from the following data library: `HistData`
- To install packages, use the following code: `install.packages("NAME.OF.PACKAGE")`
- To load the package after installation, use the following code: `library(NAME.OF.PACKAGE)`
A. Please load the data into R as a tibble (`as_tibble`) and describe the data structure. Please report how many rows/ columns of data are there (don't count them, use code to figure this out), what is the `class` of each column of the data?
```{r}
#load the tidyverse package by executing the code below
#install.packages("tidyverse")
library(tidyverse)
Virginis.interp <- as_tibble(Virginis.interp)
nrow(Virginis.interp)
ncol(Virginis.interp)
#??HistData::Virginis.interp
```
- The data structure is a 14 x 4 tibble with 14 rows of data and 3 columns labeled *year, posangle, distance,* and *velocity. Note: you could have used the `??HistData::Virginis.interp`* command to get more information on the data. In R, if you are ever confused or concerned about a command, use `??` in front of your command to get more information about it in the Help window pane.
B. Please take the average and median of the following columns: *posangel*, *distance*, and *velocity*. Report the range of years in the data as well.
```{r}
mean(Virginis.interp$posangle)
mean(Virginis.interp$distance)
mean(Virginis.interp$velocity)
median(Virginis.interp$posangle)
median(Virginis.interp$distance)
median(Virginis.interp$velocity)
range(Virginis.interp$year)
```
## Problem 2: Revisiting the Gauss
Revisit slide 20 in Lecture one and consider Gauss' algorithm:
- N(N+1)/2
We did the following in R and said N = 100
`d1 <- c(1:100)` #this code creates a sequence 1 through 100 and assigns it to the variable d1
`sum(d1) = 100 * (100 +1)/2` #this just states that the sum of d1 is equal to the sum of Gauss' formula when using 100 as our number of choice.
Write a function so that one can calculate the sum of any real number. Name your function `gauss`. After, run your function for the numbers 100, 250, and 500 and report your answers below.
NOTE: Delete the \# in front of the below text to return to code. We use \# to comment lines of code or notes to ourselves when working in R. The below is for you to fill in to make the function run.
```{r}
gauss <- function(N) {
sum <- N * (N + 1) / 2
return(sum)
}
gauss(100)
gauss(250)
gauss(500)
```
## Problem 3: Dice Rolling Function
A. In this next section we will simulate rolling fair (each side has equal probability) six-sided die. Suppose we wanted to roll one die 10 times. Since the output of any die roll is a number between 1 and 6, we could use the `sample` command . In the code box below, use the `sample` command, sampling from `1:6`, the integers between 1 and 6 inclusive, 10 times with replacement. In addition, consider what 'replacement' means and what would happen if you wrote `replace = FALSE` in your code.
- *If you set replace = FALSE in the sample function call, it means that the sampling will be done without replacement. This means that once an element is sampled, it will not be put back into the pool of possible outcomes for the subsequent selections.*
```{r}
sample(die, size = 2, replace = TRUE)
```
B. Let's now roll a six-sided die, 20 times, where the probability for rolling each side is unfair. Set the probability for each side of the day as follows:
- Side 1: 3/8
- Side 2: 1/8
- Side 3: 1/8
- Side 4: 1/8
- Side 5; 1/8
- Side 6: 1/8
hint: you'll need to use the following code in your sample command `prob = c()`
```{r}
sample(die, size = 20, replace = TRUE, prob = c(3/8, 1/8, 1/8, 1/8, 1/8, 1/8))
```
C. In this final section we will create a function to roll 2 fair (each side has equal probability) six-sided die. Write a function called `roll()` so that you you roll two dice, with replacement, and sums your two dice as the output.
hint: *reference part A of this section. In your function, you'll need to replace the \# of times rolled with any variable 'n'* . We provide the code you should be working with below.
```{r}
roll <- function () {
die <- 1:6
dice <- sample(die, size =2, replace = TRUE)
sum(dice)
}
roll() #test your function by executing the roll() command
```