rsequencerep

Repeat vector when its length is not a multiple of desired total length


I have a data frame with 1666 rows. I would like to add a column with a repeating sequence of 1:5 to use with cut() to do cross validation. It would look like this:

   Y      x1       x2       Id1
   1      .15      3.6       1
   0      1.1      2.2       2
   0      .05      3.3       3
   0      .45      2.8       4
   1      .85      3.1       5
   1      1.01     2.9       1
  ...      ...     ...      ...

Solution

  • Something, like this?

    df <- data.frame(rnorm(1666))
    df$cutter <- rep(1:5, length.out=1666)
    
    tail(df)
         rnorm.1666. cutter
    1661  0.11693169      1
    1662 -1.12508091      2
    1663  0.25441847      3
    1664 -0.06045037      4
    1665 -0.17242921      5
    1666 -0.85366242      1