Before proceeding, please review Chapter 2 of An Introduction to R and the internal R Documentation for the `seq()`

function (type `?seq`

in the R console).

Solutions are available here.

**Excercise 1**

Using the seq() function, generate the sequence `2, 5, 8, 11`

.

**Exercise 2**

Use the `seq()`

function to generate the sequence `9, 18, 27, 36, 45`

.

**Exercise 3**

Generate the sequence `9, 18, 27, 36, 45, 54, 63, 72, 81, 90`

using the `length.out`

parameter.

**Exercise 4**

For this exercise, first write down your answer, without using R. Then, check your answer using R.

What is the output for the code:

`seq(from = -10, to = 10, length.out = 5)`

**Exercise 5**

Assign value 5 to variable x.

Write code `1:x-1`

you should get `0, 1, 2, 3, 4`

.

Write code `1 : (x-1)`

you will get `1, 2, 3, 4`

.

Explain the discrepancy in the output.

**Exercise 6**

For this exercise, first write down your answer, without using R. Then, check your answer using R.

Create a vector a with values `1, 2, 3, 4`

For the code `seq(along.with = a)`

, what will be the output?

**Exercise 7**

For this exercise, first write down your answer, without using R. Then, check your answer using R.

Generate a sequence using the below code.

`seq(from=1, to=4, by=1)`

What other ways can you generate the same sequence?

a. `X <- 1:4`

b. `X <- seq(4)`

c. `X <- c(1,2,3,4)`

d. `All of the above`

**Exercise 8**

Generate a backward sequence from `5, 4, 3, 2, 1`

**Exercise 9**

Assign `x <- c(1, 2, 3, 4)`

Using the function `rep()`

, create the below sequence

`1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4`

**Exercise 10**

Assign `x <- c(1, 2, 3, 4)`

Using the `rep()`

function generate the sequence:

`1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4`

**Want to practice regular sequences a bit more? We have more exercise sets on this topic here.**

**What's next:**

- Explore all our (>1000) R exercises
- Find an R course using our R Course Finder directory
- Subscribe to receive weekly updates and bonus sets by email
- Share with your friends and colleagues using the buttons below

## Leave a Reply