E004: Vectors & Sequences

Author

Hubert Baechli

Published

March 8, 2025

Programming exercise

Below are some exercises that should help you learn the most important basic built in functions and how to incorporate them into your own functions. This sheet focuses on vectors and how the easily generated.

1. by Sequences (easy)

The most challenging aspect of R is probably its flexibility. All these lines do exactly the same thing!

vec_z <- 1:6
vec_z <- seq(1, 6)
vec_z <- seq(1, 6, 1)
vec_z <- seq(from = 1, to = 6, by = 1)
vec_z <- seq_len(6)
vec_z
[1] 1 2 3 4 5 6

Try to generate this sequence

[1] 1 3 5

2. by Repetitions (medium)

A second way to generate vectors is by using this:

rep(x = 5, times = 4)
[1] 5 5 5 5

Try to generate this sequence

[1] 1 3 1 3 1 3 1 3

And try now to generate this sequence with the same function

Hint: you will need a other parameter then times (?rep)

[1] 1 1 1 1 3 3 3 3

3. Repetitions each (medium)

As you can see, the built-in functions are very flexible and can define many diferent vectors. Now for something trickier.

Create a function that takes a single integer n and generates a sequence with all natural numbers from 1 to n, such that each number x is repeated x number of times. Here are some examples of desired output:

incremental_seq(1)
[1] 1
incremental_seq(2)
[1] 1 2 2
incremental_seq(3)
[1] 1 2 2 3 3 3
incremental_seq(4)
 [1] 1 2 2 3 3 3 4 4 4 4
incremental_seq(5)
 [1] 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
Tip

Read carefully the documentation for the rep function by typing ?rep or help(rep) in the R console. Specifically look at the explanation of the times argument in the Details section. What happens when times is not a single number but a vector instead?

4. Count repetitions (harder)

In this task you have to do almost the reverse of the previous. Given a vector of items, determine how often each item is repeated in an uninterupted sequence and print a summary. Example output:

summarise_reps(c("A", "A", "A", "B", "C", "C", "M", "M", "M", "M", "A", "A"))
Value: A, Reps: 3
Value: B, Reps: 1
Value: C, Reps: 2
Value: M, Reps: 4
Value: A, Reps: 2
Tip

You can do this with low-level functions. You could also search for a concept called “Run Length Encoding”. Is there a base R function that computes the run length encoding? What type of object does it return? How can you extract the information you need from that object?