**R-exercises**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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:

`x <- c(157, 158, 159, 160, 161, 162, 163, 164)`

`x <- c(15, 16, 17, 18, 20, 21, 22, 23, 24)`

`x <- c(10, 9, 8, 7, 6, 5, 4, 3, 2, 1)`

`x <- c(-1071, -1072, -1073, -1074, -1075, -1074, -1073, -1072, -1071)`

`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.

`(10:20) * 2`

`105:(30 * 3)`

`10:20*2`

`1 + 1:10/10`

`2^(0:5)`

(Solution)

### Exercise 3

Use the `:`

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

`x <- c(5, 10, 15, 20, 25, 30)`

`x <- c(0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3)`

`x <- c(1/5, 2/6, 3/7, 4/8, 5/9, 6/10, 7/11, 8/12)`

`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.

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

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

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

`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`

:

`x <- c(5, 5, 5, 5, 5, 5, 5)`

`x <- c(5, 6, 7)`

`y <- c(x, x, x, x, x)`

`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`

:

`x <- c(5, 5, 5, 5, 8, 8, 8, 8, -3, -3, -3, -3, 0.34, 0.34, 0.34, 0.34)`

`x <- c(-0.1, -0.1, -0.9, -0.9, -0.6, -0.6)`

`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:

`x <- c(97, 98, 99, 100, 101, 102, 97, 98, 99, 100, 101, 102, 97, 98, 99, 100, 101, 102)`

`x <- c(-5, -5, -5, -5, -6, -6, -6, -6, -7, -7, -7, -7, -8, -8, -8, -8)`

`x <- c(13, 13, 17, 17, 21, 21, 25, 25, 29, 29, 13, 13, 17, 17, 21, 21, 25, 25, 29, 29)`

`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.

- 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. - 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? - Now, let’s create vector
`h`

and`d`

using the same logic. Check that`s`

,`m`

,`h`

, and`d`

have equal length.

(Solution)

**leave a comment**for the author, please follow the link and comment on their blog:

**R-exercises**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.