11 Case study: rep()

11.1 What does rep() do?

rep() is an extremely useful base R function that repeats a vector x in various ways. It has three details arguments: times, each, and length.out3 that interact in complicated ways. Let’s explore the basics first:

times and length.out replicate the vector in the same way, but length.out allows you to specify a non-integer number of replications. If you specify both, length.out wins.

The each argument repeats individual components of the vector rather than the whole vector:

And you can combine that with times:

If you supply a vector to times it works a similar way to each, repeating each component the specified number of times:

11.2 What makes this function hard to understand?

I think these two problems have the same underlying cause: rep() is trying to do too much in a single function. I think we can make things simpler by turning rep() into two functions: one that replicates the full vector, and one that replicates each element of the vector.

11.3 How might we improve the situation?

Two create two new functions, we need to first come up with names: I like rep_each() and rep_full(). rep_each() was a fairly easy name to come up with. rep_full() was a little harder and took a few iterations: I like that full has the same number of letters as each, which makes the two functions look like they belong together.

Next, we need to think about their arguments. Both will have a single data argument: x, the vector to replicate. rep_each() has a single details argument which specifies the number of times to replicate each element. rep_time() has two mutually exclusive details arguments, the number of times to repeat the whole vector, or the desired length of the output.

What should we call the arguments? We’ve already captured the different replication strategies (each vs. full) in the function name, so I think the argument that specifies the number of times to replicate can be the same, and times seems reasonable. For the second argument to rep_full(), I draw inspiration from rep() which uses length.out. I think it’s obvious that the argument controls the output, so length is adequate.

The implementation of rep_full() and rep_each() in terms of rep.int() and rep_len() suggests that R-core members are aware of the problem.

(Note the downside of using length as the argument name: we have to call base::length() to avoid evaluating the missing length when times is supplied.)

One downside of this approach is if you want to both replicate each component and the entire vector, you have to use two function calls, which is much more verbose than the rep() equivalent. However, I don’t think this is a terribly common use case, and so I think a longer call is more readable.

That said, one argument for a single rep function is that rep_each() and rep_full() return the same result if you change their order (i.e. they’re commutative):

  1. Note that the function specification is rep(x, ...), and times, each, and length.out do not appear explicitly. You have to read the documentation to discover these arguments.