### Solution 1

#### Part a

```
x <- 157:164
x
```

`## [1] 157 158 159 160 161 162 163 164`

#### Part b

```
x <- c(15:18, 20:24)
x
```

`## [1] 15 16 17 18 20 21 22 23 24`

#### Part c

```
x <- 10:1
x
```

`## [1] 10 9 8 7 6 5 4 3 2 1`

#### Part d

```
x <- -c(1071:1075, 1074:1071)
x
```

`## [1] -1071 -1072 -1073 -1074 -1075 -1074 -1073 -1072 -1071`

Note that `x <- c(-1071:-1075, -1074:-1071)`

is also possible, but notation requires more `-`

signs.

### Solution 2

#### Part a

Note that R first evaluates expressions between parentheses (`10:20`

), and then multiplies each element of the vector by `2`

. This is an example how to create a sequence with increments larger than 1.

`(10:20) * 2`

`## [1] 20 22 24 26 28 30 32 34 36 38 40`

#### Part b

Again, R first evaluates expressions between parentheses (`10*3`

), and then creates the sequence `105:90`

.

`105:(30 * 3)`

`## [1] 105 104 103 102 101 100 99 98 97 96 95 94 93 92 91 90`

#### Part c

Note that R gives precedence to the `:`

operator, so the solution of part c is equal to that of part 1

`10:20 * 2`

`## [1] 20 22 24 26 28 30 32 34 36 38 40`

#### Part d

R first generates the sequence 1, 2, 3, …, 10, then divides by 10 (yielding 0.1, 0.2, 0.3, …, 1), and finally adds 1. This is an example how to create sequences with increments smaller than 1.

`1 + 1:10/10`

`## [1] 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0`

#### Part e

R first generates the sequence 0, 1, 2, 3, 4, 5 then raises 2 to the power of these number, so 0^2, 1^2, 2^2 etc. This is an example how to create a sequence with non-constant increments.

`2^(0:5)`

`## [1] 1 2 4 8 16 32`

### Solution 3

#### Part a

`x <- (1:6) * 5`

#### Part b

`x <- 0:15%%4`

#### Part c

`x <- (1:8)/(5:12)`

### Solution 4

#### Part a

This is a straightforward sequence starting at 20, and ending at 80, with step-size of 20, also: 20, 40, 60, 80

`seq(from = 20, to = 80, by = 20)`

`## [1] 20 40 60 80`

#### Part b

This sequence includes negatives (starting at -10), with step-size of 0.5, also: -10, -9.5, -9, …, 4.5, 5

`seq(from = -10, to = 5, by = 0.5)`

```
## [1] -10.0 -9.5 -9.0 -8.5 -8.0 -7.5 -7.0 -6.5 -6.0 -5.5 -5.0
## [12] -4.5 -4.0 -3.5 -3.0 -2.5 -2.0 -1.5 -1.0 -0.5 0.0 0.5
## [23] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0
```

#### Part c

This is a reversed sequence, starting at 10 and, ending at -2. Note two things: First, a reversed sequence requires a negative step-size, and second: due to the step-size of -2, the sequence will end at -2 instead of -3. Also: 10, 8, 6, 4, 2, 0, -2.

`seq(from = 10, to = -3, by = -2)`

`## [1] 10 8 6 4 2 0 -2`

#### Part d

This is a sequence, starting at 0.01 and, ending at 0.09, with step-size of 0.02: 0.01, 0.03, 0.05, 0.07, 0.09.

`seq(from = 0.01, to = 0.09, by = 0.02)`

`## [1] 0.01 0.03 0.05 0.07 0.09`

### Solution 5

The main insight here is that you can convert any regular sequence with step-size `z`

, to a regular sequence with step-size 1, by first dividing the start and endpoints by the step-size, thus:

starting point: `(x/z)`

ending point `(y/z)`

This gives the sequence (with `:`

notation): `(x/z):(y/z)`

Now, we can multiply each of the values in this sequence by the step-size to obtain the original sequence: `z*((x/z):(y/z))`

### Solution 6

The solution for this exercise is available in our eBook Start Here To Learn R – vol. 1: Vectors, arithmetic, and regular sequences.

### Solution 7

The solution for this exercise is available in our eBook Start Here To Learn R – vol. 1: Vectors, arithmetic, and regular sequences.

### Solution 8

The solution for this exercise is available in our eBook Start Here To Learn R – vol. 1: Vectors, arithmetic, and regular sequences.

## Leave a Reply