Regular Sequences

So far in this series, we used vectors from built-in datasets (rivers, women and nhtemp), or created them by stringing together several numbers with the c function (e.g. c(1, 2, 3, 4)). R offers an extremely useful shortcut to create vectors of the latter kind, which is the colon : operator. Instead of having to type:

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

we can simply type

x <- 1:4

to create exactly the same vector. Obviously this is especially useful for longer sequences.

In fact, you will use sequences like this a lot in real-world applications of R, e.g. to select subsets of data points, records, or variables. The exercises in this set might come across as a little abstract, but trust me, these sequences are really the basic building blocks for your future R scripts. So let’s go ahead!

Before starting the exercises, please note this is the fourth set in a series of five: In the first three sets, we practised creating vectors, vector arithmetics, and various functions. You can find all sets in our ebook Start Here To Learn R – vol. 1: Vectors, arithmetic, and regular sequences. The book also includes all solutions (carefully explained), and the fifth and final set of the series. This final set focuses on the application of the concepts you learned in the first four sets, to real-world data.

One more thing: I would really appreciate your feedback on these exercises: Which ones did you like? Which ones were too easy or too difficult? Please let me know what you think here!

Exercise 1

Try to shorten the notation of the following vectors as much as possible, using : notation:

  1. x <- c(157, 158, 159, 160, 161, 162, 163, 164)
  2. x <- c(15, 16, 17, 18, 20, 21, 22, 23, 24)
  3. x <- c(10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
  4. x <- c(-1071, -1072, -1073, -1074, -1075, -1074, -1073, -1072, -1071)
  5. x <- c(1.5, 2.5, 3.5, 4.5, 5.5)

(Solution)

Exercise 2

The : operator can be used in more complex operations along with arithmetic operators, and variable names. Have a look at the following expressions, and write down what sequence you think they will generate. Then check with R.

  1. (10:20) * 2
  2. 105:(30 * 3)
  3. 10:20*2
  4. 1 + 1:10/10
  5. 2^(0:5)

(Solution)

Exercise 3

Use the : operator and arithmetic operators/functions from the previous chapter to create the following vectors:

  1. x <- c(5, 10, 15, 20, 25, 30)
  2. x <- c(0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3)
  3. x <- c(1/5, 2/6, 3/7, 4/8, 5/9, 6/10, 7/11, 8/12)
  4. x <- (1, 4, 3, 8, 5, 12, 7, 16, 9, 20) (Hint: you have to use the recycle rule)

(Solution)

Exercise 4

Another way to generate a sequence is the seq function. Its first two arguments are from and to, followed by a third, which is by. seq(from=5, to=30, by=5) replicates part (a) of the previous exercise.

Note that you can omit the argument names from, to, and by, if you stick to their positions, i.e., seq(5, 30, 5). Have a look at the following expressions, and write down what sequence you think they will generate. Then check with R.

  1. seq(from=20, to=80, by=20)
  2. seq(from=-10, to=5, by=0.5)
  3. seq(from=10, to=-3, by=-2)
  4. seq(from=0.01, to=0.09, by=0.02)

(Solution)

Exercise 5

Compare the regular sequence of exercises 2(a) and 3(a) (both using the : operator) with the same sequences using the seq function with appropriate by argument. Can you think of a more general rule how to convert any seq(from, to, by) statement to a sequence generated with the : operator?

In other words, rewrite seq(from=x, to=y, by=z) to a statement using the : operator. Hint: if this appears difficult, try to do this first by choosing some values for x, y, and z, and see which pattern emerges.

(Solution)

Exercise 6

The previous exercises in this set were aimed at generating sets of increasing or decreasing numbers. However, sometimes you just want a set of equal numbers. You can accomplish this with the rep function (from “replicate”). Its first argument is the number or vector that will be replicated, and its second argument times, … well I guess you can guess that one already. Now, let’s shorten the following statements, using rep:

  1. x <- c(5, 5, 5, 5, 5, 5, 5)
  2. x <- c(5, 6, 7)y <- c(x, x, x, x, x)
  3. x <- (10, 16, 71, 10, 16, 71, 10, 16, 71)

(Solution)

Exercise 7

rep has a third very useful argument: each. As we saw in the previous exercise (part b), vectors are replicated in their entirety by rep.

However, you can also replicate “each” individual element. Consider for example:

seq(c(1, 2, 3), times=2, each=3).

This says: “replicate each element of the input vector c(1, 2, 3) 3 times, and then replicate the resulting vector 2 times.” Now, let’s shorten the following statements, using rep:

  1. x <- c(5, 5, 5, 5, 8, 8, 8, 8, -3, -3, -3, -3, 0.34, 0.34, 0.34, 0.34)
  2. x <- c(-0.1, -0.1, -0.9, -0.9, -0.6, -0.6)
  3. x <- c(1, 1, 2, 2, 3, 3, 1, 1, 2, 2, 3, 3, 1, 1, 2, 2, 3, 3)

(Solution)

Exercise 8

We can actually write part c of te previous exercise even more compact by using rep in combination with the : operator. Do you see how?

In this exercise we’re using combinations of rep, : and seq to create the following sequences:

  1. x <- c(97, 98, 99, 100, 101, 102, 97, 98, 99, 100, 101, 102, 97, 98, 99, 100, 101, 102)
  2. x <- c(-5, -5, -5, -5, -6, -6, -6, -6, -7, -7, -7, -7, -8, -8, -8, -8)
  3. x <- c(13, 13, 17, 17, 21, 21, 25, 25, 29, 29, 13, 13, 17, 17, 21, 21, 25, 25, 29, 29)
  4. x <- c(1, 2, 3, 2, 1, 0, 1, 2, 3, 2, 1, 0, 1, 2, 3, 2, 1, 0)

(Solution)

Exercise 9

Suppose there would be no each argument for rep. Rewrite the following statement, without using the each argument: x <- rep(c(27, 31, 19, 14), each=v, times=w)

(Solution)

Exercise 10

Let’s finish this set off with an application. Let’s create a series of vectors for later use in a timeseries dataset. The idea is that each observation in this dataset can be identified by a timestamp, which is defined by four vectors:

  • s (for seconds)
  • m (minutes)
  • h (hours)
  • d (days)

For this exercise, we’ll limit the series to a full week of 7 days.

This is a somewhat more complicated problem than the previous ones in this exercise. Don’t worry however! Whenever you’re faced with a somewhat more complicated problem than you are used to, the best strategy is to break it down into smaller problems. So, we’ll simply start with the s vector.

  1. Since s counts the number of seconds, we know it has to start at 1, run to 60, restart at 1, etc. As it should cover a full week, we also know we have to replicate this series many times. Can you calculate exactly how many times it has to replicate this series? Use the outcome of your calculation to create the full s vector.
  2. Now, let’s create the vector m. Think about how this vector differs from s. What does this mean for the times and each arguments?
  3. Now, let’s create vector h and d using the same logic. Check that s, m, h, and d have equal length.

(Solution)




Regular Sequences Vol. 2 Exercises

regular sequence

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

Answers to the exercises are available here.

Exercise 1

if x <- c(a = 1, b = 2,c=3,d=4)
What is the output for the code:
seq(5,11,along.with =x)

Exercise 2

If x= seq(4,12,4) ,
what is the output for the code:
rep(x,each=2)

Exercise 3

What is the output for the code:
seq(5,11,by=2,length.out=3)

Exercise 4

What is the output for the code:
rep(letters[1:10],3)

Exercise 5

Create a sequence with values:
100 95 90 85 80 75 70 65 60 55 50

Exercise 6

What is the output for the code:
seq(10,0,by=5)

Exercise 7

What is the output for the code:
seq(2,10,by=4)==c(2,6,10)

Exercise 8

What is the output for the code:
 rep(c('seq','rep'),each=4)

Exercise 9

Consider two variables, A and B,
A= as.Date("2016-11-01")

B = as.Date("2016-11-15")

What is the output for the code:
seq.Date(A,B, by = "1 day")

Exercise 10
Consider two variables, C and D,

C= as.Date("2016-02-01")

D = as.Date("2016-06-15")

What is the output for the code:
seq.Date(D,C, by = "-1 month")

 

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




Regular sequences

regular sequenceBefore 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.