rdplyrsequential-number

dplyr: Mutate a new column with sequential repeated integers of n time in a dataframe


I am struggling with one maybe easy question. I have a dataframe of 1 column with n rows (n is a multiple of 3). I would like to add a second column with integers like: 1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,.. How can I achieve this with dplyr as a general solution for different length of rows (all multiple of 3).

I tried this:

df <- tibble(Col1 = c(1:12)) %>% 
  mutate(Col2 = rep(1:4, each=3))

This works. But I would like to have a solution for n rows, each = 3 . Many thanks!


Solution

  • You can specify each and length.out parameter in rep.

    library(dplyr)
    
    tibble(Col1 = c(1:12)) %>% 
      mutate(Col2 = rep(row_number(), each=3, length.out = n()))
    
    #    Col1  Col2
    #   <int> <int>
    # 1     1     1
    # 2     2     1
    # 3     3     1
    # 4     4     2
    # 5     5     2
    # 6     6     2
    # 7     7     3
    # 8     8     3
    # 9     9     3
    #10    10     4
    #11    11     4
    #12    12     4