-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
05-scalersvectors.Rmd
562 lines (369 loc) · 23.6 KB
/
05-scalersvectors.Rmd
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
---
output:
pdf_document: default
html_document: default
word_document: default
---
# Scalars and vectors {#scalersvectors}
```{r, echo = FALSE}
knitr::opts_chunk$set(collapse = TRUE)
library(yarrr)
```
```{r scalervector, echo = FALSE, fig.cap= "Visual depiction of a scalar and vector. Deep shit. Wait until we get to matrices - you're going to lose it.", fig.align="center", fig.width = 2, fig.height= 4}
par(mar = rep(1, 4))
plot(1, xlim = c(0, 4), ylim = c(-.5, 5),
xlab = "", ylab = "",
xaxt = "n", yaxt = "n",
bty = "n", type = "n")
# scalar
rect(rep(0, 1), rep(0, 1), rep(1, 1), rep(1, 1))
text(.5, -.5, "Scalar")
# Vector
rect(rep(2, 5), 0:4, rep(3, 5), 1:5)
text(2.5, -.5, "Vector")
```
```{r}
# Crew information
captain.name <- "Jack"
captain.age <- 33
crew.names <- c("Heath", "Vincent", "Maya", "Becki")
crew.ages <- c(19, 35, 22, 44)
crew.sex <- c(rep("M", times = 2), rep("F", times = 2))
crew.ages.decade <- crew.ages / 10
# Earnings over first 10 days at sea
days <- 1:10
gold <- seq(from = 10, to = 100, by = 10)
silver <- rep(50, times = 10)
total <- gold + silver
```
People are not objects. But R is full of them. Here are some of the basic ones.
## Scalars
The simplest object type in R is a **scalar**. A scalar object is just a single value like a number or a name. In the previous chapter we defined several scalar objects. Here are examples of numeric scalars:
```{r}
# Examples of numeric scalars
a <- 100
b <- 3 / 100
c <- (a + b) / b
```
Scalars don't have to be numeric, they can also be **characters** (also known as strings). In R, you denote characters using quotation marks. Here are examples of character scalars:
```{r}
# Examples of character scalars
d <- "ship"
e <- "cannon"
f <- "Do any modern armies still use cannons?"
```
As you can imagine, R treats numeric and character scalars differently. For example, while you can do basic arithmetic operations on numeric scalars -- they won't work on character scalars. If you try to perform numeric operations (like addition) on character scalars, you'll get an error like this one:
```{r, eval = FALSE}
a <- "1"
b <- "2"
a + b
```
<div class="error">Error in a + b: non-numeric argument to binary operator</div>
If you see an error like this one, it means that you're trying to apply numeric operations to character objects. That's just sick and wrong.
##Vectors
Now let's move onto `vectors`. A vector object is just a combination of several scalars stored as a single object. For example, the numbers from one to ten could be a vector of length 10, and the characters in the English alphabet could be a vector of length 26. Like scalars, vectors can be either numeric or character (but not both!).
There are many ways to create vectors in R. Here are the methods we will cover in this chapter:
| Function| Example|Result |
|:-------------------------|:-----------------------------|:----------|
| `c(a, b, ...)`| `c(1, 5, 9)` |`r c(1, 5, 9)` |
| `a:b`| `1:5`|`r 1:5` |
| `seq(from, to, by, length.out)`| `seq(from = 0, to = 6, by = 2)`|`r seq(from = 0, to = 6, by = 2)` |
| `rep(x, times, each, length.out)`| `rep(c(7, 8), times = 2, each = 2)`|`r rep(c(7, 8), times = 2, each = 2)` |
Table: Functions to create vectors.
The simplest way to create a vector is with the `c()` function. The c here stands for concatenate, which means "bring them together". The `c()` function takes several scalars as arguments, and returns a vector containing those objects. When using c(), place a comma in between the objects (scalars or vectors) you want to combine:
Let's use the `c()` function to create a vector called `a` containing the integers from 1 to 5.
```{r}
# Create an object a with the integers from 1 to 5
a <- c(1, 2, 3, 4, 5)
# Print the result
a
```
As you can see, R has stored all 5 numbers in the object `a`. Thanks R!
You can also create longer vectors by combining vectors you have already defined. Let's create a vector of the numbers from 1 to 10 by first generating a vector `a` from 1 to 5, and a vector `b` from 6 to 10 then combine them into a single vector `x`:
```{r}
a <- c(1, 2, 3, 4, 5)
b <- c(6, 7, 8, 9, 10)
x <- c(a, b)
x
```
You can also create character vectors by using the `c()` function to combine character scalars into character vectors:
```{r, fig.cap= "This is not a pipe. It is a character vector.", fig.margin = TRUE, echo = FALSE, out.width = "50%", fig.align='center'}
knitr::include_graphics(c("images/magrittepipe.jpg"))
```
```{r}
char.vec <- c("Ceci", "nest", "pas", "une", "pipe")
char.vec
```
While the `c()` function is the most straightforward way to create a vector, it's also one of the most tedious. For example, let's say you wanted to create a vector of all integers from 1 to 100. You definitely don't want to have to type all the numbers into a c() operator. Thankfully, R has many simple built-in functions for generating numeric vectors. Let's start with three of them: `a:b`, `seq()`, and `rep()`:
### a:b
The `a:b` function takes two numeric scalars `a` and `b` as arguments, and returns a vector of numbers from the starting point `a` to the ending point `b` in steps of 1.
Here are some examples of the `a:b` function in action. As you'll see, you can go backwards or forwards, or make sequences between non-integers:
```{r}
1:10
10:1
2.5:8.5
```
###seq()
| Argument| Definition|
|:-------------------------|:-----------------------------|
| `from`| The start of the sequence |
| `to`| The end of the sequence |
| `by`| The step-size of the sequence|
| `length.out`| The desired length of the final sequence (only use if you don't specify `by`)|
The `seq()` function is a more flexible version of `a:b`. Like `a:b`, `seq()` allows you to create a sequence from a starting number to an ending number. However, `seq()` has additional arguments that allow you to specify either the size of the steps between numbers, or the total length of the sequence.
The `seq()` function has two new arguments: `by` and `length.out`. If you use the `by` argument, the sequence will be in steps of the input to the `by` argument:
```{r}
# Create the numbers from 1 to 10 in steps of 1
seq(from = 1, to = 10, by = 1)
# Integers from 0 to 100 in steps of 10
seq(from = 0, to = 100, by = 10)
```
If you use the `length.out` argument, the sequence will have a length equal to `length.out`.
```{r}
# Create 10 numbers from 1 to 5
seq(from = 1, to = 5, length.out = 10)
# 3 numbers from 0 to 100
seq(from = 0, to = 100, length.out = 3)
```
###rep()
| Argument| Definition|
|:-------------------------|:-----------------------------|
| `x`| A scalar or vector of values to repeat |
| `times`| The number of times to repeat x |
| `each`| The number of times to repeat each value within x|
| `length.out`| The desired length of the final sequence|
```{r rep, fig.cap= "Not a good depiction of a rep in R.", fig.margin = TRUE, echo = FALSE, out.width = "50%", fig.align='center'}
knitr::include_graphics(c("images/rep.jpg"))
```
The `rep()` function allows you to repeat a scalar (or vector) a specified number of times, or to a desired length. Let's do some reps.
```{r}
rep(x = 3, times = 10)
rep(x = c(1, 2), each = 3)
rep(x = 1:3, length.out = 10)
```
As you can see, you can can include an `a:b` call within a `rep()`!
You can even combine the `times` and `each` arguments within a single `rep()` function. For example, here's how to create the sequence {1, 1, 2, 2, 3, 3, 1, 1, 2, 2, 3, 3} with one call to `rep()`:
```{r}
rep(x = 1:3, each = 2, times = 2)
```
**Warning! Vectors contain either numbers or characters, not both**
A vector can only contain one type of scalar: either numeric or character. If you try to create a vector with numeric and character scalars, then R will convert *all* of the numeric scalars to characters. In the next code chunk, I'll create a new vector called `my.vec` that contains a mixture of numeric and character scalars.
```{r}
my.vec <- c("a", 1, "b", 2, "c", 3)
my.vec
```
As you can see from the output, `my.vec` is stored as a character vector where all the numbers are converted to characters.
## Generating random data
Because R is a language built for statistics, it contains many functions that allow you to generate random data -- either from a vector of data that you specify (like Heads or Tails from a coin), or from an established *probability distribution*, like the Normal or Uniform distribution.
In the next section we'll go over the standard `sample()` function for drawing random values from a vector. We'll then cover some of the most commonly used probability distributions: Normal and Uniform.
### sample()
| Argument| Definition|
|:---------|:---------------------------------------------|
| `x`| A vector of outcomes you want to sample from. For example, to simulate coin flips, you'd enter `x = c("H", "T")` |
| `size`| The number of samples you want to draw. The default is the length of `x`.|
| `replace`| Should sampling be done with replacement? If FALSE (the default value), then each outcome in `x` can only be drawn once. If TRUE, then each outcome in `x` can be drawn multiple times.|
| `prob`| A vector of probabilities of the same length as `x` indicating how likely each outcome in `x` is. The vector of probabilities you give as an argument should add up to one. If you don't specify the `prob` argument, all outcomes will be equally likely.|
The `sample()` function allows you to draw random samples of elements (scalars) from a vector. For example, if you want to simulate the 100 flips of a fair coin, you can tell the sample function to sample 100 values from the vector ["Heads", "Tails"]. Or, if you need to randomly assign people to either a "Control" or "Test" condition in an experiment, you can randomly sample values from the vector ["Control", "Test"]:
Let's use `sample()` to draw 10 samples from a vector of integers from 1 to 10.
```{r}
# From the integers 1:10, draw 5 numbers
sample(x = 1:10, size = 5)
```
#### replace = TRUE
If you don't specify the `replace` argument, R will assume that you are sampling *without* replacement. In other words, each element can only be sampled once. If you want to sample with replacement, use the `replace = TRUE` argument:
Think about replacement like drawing balls from a bag. Sampling *with* replacement (`replace = TRUE`) means that each time you draw a ball, you return the ball back into the bag before drawing another ball. Sampling *without* replacement (`replace = FALSE`) means that after you draw a ball, you remove that ball from the bag so you can never draw it again.
```{r}
# Draw 30 samples from the integers 1:5 with replacement
sample(x = 1:5, size = 10, replace = TRUE)
```
If you try to draw a large sample from a vector \textit{without} replacement, R will return an error because it runs out of things to draw:
```{r eval = FALSE}
# You CAN'T draw 10 samples without replacement from
# a vector with length 5
sample(x = 1:5, size = 10)
```
<div class="error">Error: cannot take a sample larger than the population when 'replace = FALSE'</div>
To fix this, just tell R that you want to sample with replacement:
```{r}
# You CAN draw 10 samples with replacement from a
# vector of length 5
sample(x = 1:5, size = 10, replace = TRUE)
```
To specify how likely each element in the vector `x` should be selected, use the `prob` argument. The length of the `prob` argument should be as long as the `x` argument. For example, let's draw 10 samples (with replacement) from the vector ["a", "b"], but we'll make the probability of selecting "a" to be .90, and the probability of selecting "b" to be .10
```{r}
sample(x = c("a", "b"),
prob = c(.9, .1),
size = 10,
replace = TRUE)
```
#### Ex: Simulating coin flips
Let's simulate 10 flips of a fair coin, where the probably of getting either a Head or Tail is .50. Because all values are equally likely, we don't need to specify the `prob` argument:
```{r}
sample(x = c("H", "T"), # The possible values of the coin
size = 10, # 10 flips
replace = TRUE) # Sampling with replacement
```
Now let's change it by simulating flips of a biased coin, where the probability of Heads is 0.8, and the probability of Tails is 0.2. Because the probabilities of each outcome are no longer equal, we'll need to specify them with the `prob` argument:
```{r}
sample(x = c("H", "T"),
prob = c(.8, .2), # Make the coin biased for Heads
size = 10,
replace = TRUE)
```
As you can see, our function returned a vector of 10 values corresponding to our sample size of 10.
#### Ex: Coins from a chest
```{r, echo = FALSE}
par(mar = c(3, 3, 3, 3))
plot(1, xlim = c(0, 1), ylim = c(0, 1),
xlab = "", ylab = "", xaxt = "n",
yaxt = "n", type = "n",
main = "Chest of 20 Gold, 30 Silver,\nand 50 Bronze Coins")
points(runif(100, .1, .9),
runif(100, .1, .9),
pch = 21, cex = 3,
bg = c(rep("gold", 20),
rep("gray94", 30),
rep("chocolate", 50))
)
```
Now, let's sample drawing coins from a treasure chest. Let's say the chest has 100 coins: 20 gold, 30 silver, and 50 bronze. Let's draw 10 random coins from this chest.
```{r}
# Create chest with the 100 coins
chest <- c(rep("gold", 20),
rep("silver", 30),
rep("bronze", 50))
# Draw 10 coins from the chest
sample(x = chest,
size = 10)
```
The output of the `sample()` function above is a vector of 10 strings indicating the type of coin we drew on each sample. And like any random sampling function, this code will likely give you different results every time you run it! See how long it takes you to get 10 gold coins...
In the next section, we'll cover how to generate random data from specified *probability distributions*. What is a probability distribution? Well, it's simply an equation -- also called a likelihood function -- that indicates how likely certain numerical values are to be drawn.
We can use probability distributions to represent different types of data. For example, imagine you need to hire a new group of pirates for your crew. You have the option of hiring people from one of two different pirate training colleges that produce pirates of varying quality. One college "Pirate Training Unlimited" might tend to pirates that are generally ok - never great but never terrible. While another college "Unlimited Pirate Training" might produce pirates with a wide variety of quality, from very low to very high. In Figure \@ref(fig:piratecollege) I plotted 5 example pirates from each college, where each pirate is shown as a ball with a number written on it. As you can see, pirates from PTU all tend to be clustered between 40 and 60 (not terrible but not great), while pirates from UPT are all over the map, from 0 to 100. We can use probability distributions (in this case, the uniform distribution) to mathematically define how likely any possible value is to be drawn at random from a distribution. We could describe Pirate Training Unlimited with a uniform distribution with a small range, and Unlimited Pirate Training with a second uniform distribution with a wide range.
```{r piratecollege, fig.cap = "Sampling 5 potential pirates from two different pirate colleges. Pirate Training Unlimited (PTU) consistently produces average pirates (with scores between 40 and 60), while Unlimited Pirate Training (UPT), produces a wide range of pirates from 10 to 90.", echo = FALSE}
# Create blank plot
plot(1, xlim = c(0, 100), ylim = c(0, 100),
xlab = "Pirate Quality", ylab = "", type = "n",
main = "Two different Pirate colleges", yaxt = "n")
# Set colors
col.vec <- yarrr::piratepal(palette = "nemo")
text(50, 90, "Pirate Training Unlimited", font = 3)
ptu <- runif(n = 5, min = 40, max = 60)
points(ptu, rep(75, 5), pch = 21, bg = col.vec[1], cex = 3)
text(ptu, rep(75, 5), round(ptu, 0))
segments(40, 65, 60, 65, col = col.vec[1], lwd = 4)
text(50, 40, "Unlimited Pirate Training", font = 3)
upt <- runif(n = 5, min = 10, max = 90)
points(upt, rep(25, 5), pch = 21, bg = col.vec[2], cex = 3)
text(upt, rep(25, 5), round(upt, 0))
segments(10, 15, 90, 15, col = col.vec[2], lwd = 4)
```
In the next two sections, I'll cover the two most common distributions: The Normal and the Uniform. However, R contains many more distributions than just these two. To see them all, look at the help menu for Distributions:
```{r eval = FALSE}
# See all distributions included in Base R
?Distributions
```
### Normal (Gaussian)
```{r normaldist, fig.cap = "Three different normal distributions with different means and standard deviations", echo = FALSE}
# Create blank plot
plot(1, xlim = c(-5, 5), ylim = c(0, 1),
xlab = "x", ylab = "dnorm(x)", type = "n",
main = "Three Normal Distributions", bty = "n")
f1 <- function(x) {dnorm(x, mean = 0, sd = 1)}
f2 <- function(x) {dnorm(x, mean = -2, sd = .5)}
f3 <- function(x) {dnorm(x, mean = 1, sd = 2)}
# Set colors
col.vec <- yarrr::piratepal("southpark")
curve(f1, from = -5, to = 5, add = TRUE, col = col.vec[1], lwd = 3)
curve(f2, from = -5, to = 5, add = TRUE, col = col.vec[2], lwd = 3)
curve(f3, from = -5, to = 5, add = TRUE, col = col.vec[3], lwd = 3)
legend(x = 0, y = 1,
legend = c(expression(paste(mu == 0, ", ", sigma == 1)),
expression(paste(mu == -2, ", ", sigma == .5)),
expression(paste(mu == 1, ", ", sigma == 2))),
lwd = rep(3, 3),
col = col.vec[1:3],
bty = "n")
```
| Argument| Definition|
|:---------|:---------------------------------------------|
| `n`| The number of observations to draw from the distribution.|
| `mean`| The mean of the distribution.|
| `sd`| The standard deviation of the distribution.|
The Normal (a.k.a "Gaussian") distribution is probably the most important distribution in all of statistics. The Normal distribution is bell-shaped, and has two parameters: a mean and a standard deviation. To generate samples from a normal distribution in R, we use the function `rnorm()`
```{r}
# 5 samples from a Normal dist with mean = 0, sd = 1
rnorm(n = 5, mean = 0, sd = 1)
# 3 samples from a Normal dist with mean = -10, sd = 15
rnorm(n = 3, mean = -10, sd = 15)
```
Again, because the sampling is done randomly, you'll get different values each time you run `rnorm()`
### Uniform
```{r uniformdist, echo = FALSE, fig.cap="The Uniform distribution - known colloquially as the Anthony Davis distribution."}
plot(1, xlim = c(-5, 5), ylim = c(0, 1.25),
main = "3 Uniform Distributions",
xlab = "x", ylab = "Density", type = "n", bty = "n")
f1 <- function(x) {dunif(x, min = -1, max = 1)}
f2 <- function(x) {dunif(x, min = -4, max = -3)}
f3 <- function(x) {dunif(x, min = -2, max = 4)}
col.vec <- yarrr::piratepal("google")
curve(f1, from = -1, to = 1, add = TRUE, col = col.vec[1], lwd = 3)
segments(c(-1, 1), c(0, 0), c(-1, 1), c(f1(-1), f1(1)), lty = 2, col = col.vec[1])
curve(f2, from = -4, to = -3, add = TRUE, col = col.vec[2], lwd = 3)
segments(c(-4, -3), c(0, 0), c(-4, -3), c(f2(-4), f2(-3)), lty = 2, col = col.vec[2])
curve(f3, from = -2, to = 4, add = TRUE, col = col.vec[3], lwd = 3)
segments(c(-2, 4), c(0, 0), c(-2, 4), c(f3(-2), f3(4)), lty = 2, col = col.vec[3])
# Add Legend
legend(x = -2, y = 1.2,
legend = c("Min = -1, Max = +1",
"Min = -4, Max = -3",
"Min = -2, Max = +4"),
lwd = rep(3, 3),
col = col.vec[1:3],
bty = "n")
```
Next, let's move on to the Uniform distribution. The Uniform distribution gives equal probability to all values between its minimum and maximum values. In other words, everything between its lower and upper bounds are equally likely to occur. To generate samples from a uniform distribution, use the function `runif()`, the function has 3 arguments:
| Argument| Definition|
|:---------|:---------------------------------------------|
| `n`| The number of observations to draw from the distribution.|
| `min`| The lower bound of the Uniform distribution from which samples are drawn|
| `max`| The upper bound of the Uniform distribution from which samples are drawn|
Here are some samples from two different Uniform distributions:
```{r}
# 5 samples from Uniform dist with bounds at 0 and 1
runif(n = 5, min = 0, max = 1)
# 10 samples from Uniform dist with bounds at -100 and +100
runif(n = 10, min = -100, max = 100)
```
### Notes on random samples
#### Random samples will always change
Every time you draw a sample from a probability distribution, you'll (likely) get a different result. For example, see what happens when I run the following two commands (you'll learn the `rnorm()` function on the next page...)
```{r}
# Draw a sample of size 5 from a normal distribution with mean 100 and sd 10
rnorm(n = 5, mean = 100, sd = 10)
# Do it again!
rnorm(n = 5, mean = 100, sd = 10)
```
As you can see, the exact same code produced different results -- and that's exactly what we want! Each time you run `rnorm()`, or another distribution function, you'll get a new random sample.
#### Use `set.seed()` to control random samples
There will be cases where you will want to exert some control over the random samples that R produces from sampling functions. For example, you may want to create a reproducible example of some code that anyone else can replicate exactly. To do this, use the `set.seed()` function. Using `set.seed()` will force R to produce consistent random samples at any time on any computer.
In the code below I'll set the sampling seed to 100 with `set.seed(100)`. I'll then run `rnorm()` twice. The results will always be consistent (because we fixed the sampling seed).
```{r}
# Fix sampling seed to 100, so the next sampling functions
# always produce the same values
set.seed(100)
# The result will always be -0.5022, 0.1315, -0.0789
rnorm(3, mean = 0, sd = 1)
# The result will always be 0.887, 0.117, 0.319
rnorm(3, mean = 0, sd = 1)
```
Try running the same code on your machine and you'll see the exact same samples that I got above. Oh and the value of 100 I used above in `set.seed(100)` is totally arbitrary -- you can set the seed to any integer you want. I just happen to like how `set.seed(100)` looks in my code.
## Test your R might!
1. Create the vector [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] in three ways: once using `c()`, once using `a:b`, and once using `seq()`.
2. Create the vector [2.1, 4.1, 6.1, 8.1] in two ways, once using `c()` and once using `seq()`
3. Create the vector [0, 5, 10, 15] in 3 ways: using `c()`, `seq()` with a `by` argument, and `seq()` with a `length.out` argument.
4. Create the vector [101, 102, 103, 200, 205, 210, 1000, 1100, 1200] using a combination of the `c()` and `seq()` functions
5. A new batch of 100 pirates are boarding your ship and need new swords. You have 10 scimitars, 40 broadswords, and 50 cutlasses that you need to distribute evenly to the 100 pirates as they board. Create a vector of length 100 where there is 1 scimitar, 4 broadswords, and 5 cutlasses in each group of 10. That is, in the first 10 elements there should be exactly 1 scimitar, 4 broadswords and 5 cutlasses. The next 10 elements should also have the same number of each sword (and so on).
6. Create a vector that repeats the integers from 1 to 5, 10 times. That is [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, ...]. The length of the vector should be 50!
7. Now, create the same vector as before, but this time repeat 1, 10 times, then 2, 10 times, etc., That is [1, 1, 1, ..., 2, 2, 2, ..., ... 5, 5, 5]. The length of the vector should also be 50
8. Create a vector containing 50 samples from a Normal distribution with a population mean of 20 and standard deviation of 2.
9. Create a vector containing 25 samples from a Uniform distribution with a lower bound of -100 and an upper bound of -50.