2.2 Constructing Patterned Vectors

Quite often we need to make lengthy vectors that follow simple patterns. R has a few functions to assist us in these tasks.

2.2.1 Sequencing

Consider the seq() function:

seq(from = 5, to = 15, by = 1)
##  [1]  5  6  7  8  9 10 11 12 13 14 15

The default value of the parameter by is 1, so we could get the same thing with:

seq(from = 5, to = 15)
##  [1]  5  6  7  8  9 10 11 12 13 14 15

Further reduction in typing may be achieved as long as we remember the order in which R expects the parameters (from before to, then by if supplied):

seq(5, 15)
##  [1]  5  6  7  8  9 10 11 12 13 14 15

Some more complex examples:

seq(3, 15, 2)
## [1]  3  5  7  9 11 13 15
seq(0, 1, 0.1)
##  [1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0

R will go up to the to value, but not past it:

seq(3, 16, 2)
## [1]  3  5  7  9 11 13 15

Negative steps are fine:

seq(5, -4, -1)
##  [1]  5  4  3  2  1  0 -1 -2 -3 -4

The colon operator : is a convenient abbreviation for seq:

1:5 # 1 is from, 5 is to
## [1] 1 2 3 4 5

If the from number is greater than the to number the step for the colon operator is -1:

5:1
## [1] 5 4 3 2 1

2.2.2 Repeating

With rep() we may repeat a given vector as many times as we like:

rep(3, times = 5)
## [1] 3 3 3 3 3

We can apply rep() to a vector of length greater than 1:

vec <- c(7, 3, 4)
rep(vec, times = 3)
## [1] 7 3 4 7 3 4 7 3 4

rep() applies perfectly well to character-vectors:

rep("Toto", 4)
## [1] "Toto" "Toto" "Toto" "Toto"

rep() also takes an each parameter that determines how many times each element of the given vector will be repeated before the times parameter is applied. This is best illustrated with an example:

vec <- c(7, 3, 4)
rep(vec, each = 2, times = 3)
##  [1] 7 7 3 3 4 4 7 7 3 3 4 4 7 7 3 3 4 4

If we combine seq() and rep() we can create fairly complex patterns concisely:

vec <- seq(5, -3, -2)
rep(vec, each = 2, times = 2)
##  [1]  5  5  3  3  1  1 -1 -1 -3 -3  5  5  3  3  1  1 -1 -1 -3 -3

In order to create fifty 10’s followed by fifty 30’s followed by fifty 50’s I would write:

rep(seq(10, 50, 20), each = 50)

2.2.3 Practice Exercises

  1. Use rep() to make the following vector:

    ## [1] "Kansas" "Kansas" "Kansas" "Kansas" "Kansas"
  2. Use rep() to make the following vector:

    ## [1]  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
  3. Use seq() to make the following vector:

    ## [1]  5  8 11 14 17 20 23 26
  4. Use seq() to make all of the multiples of 4, beginning with 8 and going down to -32.

  5. Use the colon operator to make all of the whole numbers from 10 to 20.

  6. Use the colon operator to make all of the whole numbers from 10 to -30.

  7. You have a vector named myVec. Use the colon operator and the length() function to make all of the whole numbers from 1 to the length of myVec.

  8. Use rep() and seq() together to make the following vector:

    ##  [1]  2  3  4  5  6  7  8  9 10  2  3  4  5  6  7  8  9 10  2  3  4  5  6  7  8  9 10
  9. Use rep() and seq() together to make the following vector:

    ##  [1]  2  2  2  3  3  3  4  4  4  5  5  5  6  6  6  7  7  7  8  8  8  9  9  9 10 10 10
  10. Read the Help for rep()

    ?rep

    It tells you that the first argument of rep() is the vector that you want to repeat, and that it’s called x. It goes on to say that times is:

    “an integer-valued vector giving the (non-negative) number of times to repeat each element if of length length(x), or to repeat the whole vector if of length 1.”

    Use this information to describe in words what will be the output of:

    rep(seq(10, 100, by = 10), times = 1:10)

2.2.4 Solutions to Practice Exercises

  1. Here’s how:

    rep("Kansas", times = 5)
  2. Here’s how:

    rep(c(TRUE, FALSE), times = 4)
  3. Here’s how:

    seq(5, 26, by = 3)
  4. Here’s how:

    seq(8, -32, by = -4)
  5. Here’s how:

    10:20
  6. Here’s how:

    10:-30
  7. All you need is this:

    1:length(myVec)
  8. Here’s how:

    rep(seq(2, 10), times = 3)
  9. Here’s how:

    rep(seq(2, 10), each = 3)
  10. You’ll get one 10, two 20s, three 30s, …, all the way up to ten 100s.

    rep(seq(10, 100, by = 10), times = 1:10)
    ##  [1]  10  20  20  30  30  30  40  40  40  40  50  50  50  50  50  60  60  60  60  60  60
    ## [22]  70  70  70  70  70  70  70  80  80  80  80  80  80  80  80  90  90  90  90  90  90
    ## [43]  90  90  90 100 100 100 100 100 100 100 100 100 100