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 6Hubert Baechli
March 8, 2025
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.
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 6Try to generate this sequence
[1] 1 3 5A second way to generate vectors is by using this:
Try to generate this sequence
[1] 1 3 1 3 1 3 1 3And 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 3As 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:
[1] 1[1] 1 2 2[1] 1 2 2 3 3 3 [1] 1 2 2 3 3 3 4 4 4 4 [1] 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5Read 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?
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:
Value: A, Reps: 3
Value: B, Reps: 1
Value: C, Reps: 2
Value: M, Reps: 4
Value: A, Reps: 2You 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?