Skip to main content

1.8) Some Basic Functions (cont'd)


rep() This is the easy way to create vectors with repeated values. It also shows how versatile a single function can be. The first argument can be a single value to repeat, and you use the next argument (after a comma) to specify the number of times to repeat it. Alternatively, provide a whole sequence of values (as a vector, using c()) to the first argument. Then, you can provide a number for the argument each= to have each value repeated that many times. Or you can provide a times= argument. If this is a single value, the whole sequence will be repeated that many times. If it’s another vector, of the same length as the sequence (e.g. rep(c(1,2,4), times=c(3,3,2))), then each element will be repeated the corresponding number of times before the next element appears. As a novice, you could find all this out using:
help() This function opens the help file on any function – e.g. type help(rep) and a new window opens explaining how to use rep(). help() may usually be abbreviated using just ? – e.g. ?rep (but this doesn’t work for symbols, so use help(":") for help on the use of the colon). See “Getting help” below...

Create a vector named Group that contains a sequence of the values 1 to 5 repeated 20 times.