rep                   package:base                   R Documentation

_R_e_p_l_i_c_a_t_e _E_l_e_m_e_n_t_s _o_f _V_e_c_t_o_r_s _a_n_d _L_i_s_t_s

_D_e_s_c_r_i_p_t_i_o_n:

     `rep' replicates the values in `x' according to the values given
     in `times' and `length.out'.

_U_s_a_g_e:

     rep(x, times, length.out, each)
     rep.int(x, times)

_A_r_g_u_m_e_n_t_s:

       x: a vector (of any mode including a list) or a pairlist.

   times: integer.  A vector giving the number of times to repeat each
          element if of length `length(x)', or to repeat the whole
          vector if of length 1.

length.out: integer.  (Optional.)  The desired length of the output
          vector.

    each: optional integer. Each element of `x' is repeated `each'
          times.

_D_e_t_a_i_l_s:

     If `times' consists of a single integer, the result consists of
     the values in `x' repeated this many times. If `times' is a vector
     of the same length as `x', the result consists of `x[1]' repeated
     `times[1]' times, `x[2]' repeated `times[2]' times and so on.

     `length.out' may be given in place of `times', in which case `x'
     is repeated as many times as is necessary to create a vector of
     this length.  If both `length.out' and `times' are specified,
     `times' determines the replication, and `length.out' can be used
     to truncate the output vector (or extend it by `NA's).

     Non-integer values of `times' will be truncated towards zero. If
     `times' is a computed quantity it is prudent to add a small fuzz.

_N_o_t_e:

     If the original vector has names, these are also replicated and so
     will almost always contain duplicates.

     If `length.out' is used to extend the vector, the behaviour is
     different from that of S-PLUS, which recycles the existing vector.

     Function `rep.int' is a simple case handled by internal code, and
     provided as a separate function purely for S compatibility.

_S_e_e _A_l_s_o:

     `seq', `sequence'.

_E_x_a_m_p_l_e_s:

     rep(1:4, 2)
     rep(1:4, each = 2)       # not the same.
     rep(1:4, c(2,2,2,2))     # same as second.
     rep(1:4, c(2,1,2,1))
     rep(1:4, each = 2, len = 4)  # first 4 only.
     rep(1:4, each = 2, len = 10)   # 8 integers plus two NAs

     rep(1, 40*(1-.8)) # length 7 on most platforms
     rep(1, 40*(1-.8)+1e-7) # better

     ## replicate a list
     fred <- list(happy = 1:10, name = "squash")
     rep(fred, 5)

