'Is While Loop only option for splitting a numerical vector?

I have been trying to see if dplyr or tidyr could help me with this but I am not sure. I've looked into pivot_wider, slice, mutate. I've been searching on this site and did a google search to see if this was some way I could solve my coding question, however, I had no luck.

I have a numerical vector and wanted to split it up sequentially so basically take 5 rows at a time and make them one row in another dataframe. I've seen here how to split it in half on stack overflow, but that's not what I was after. I did think of assigning them numbers and group them that way but I thought I could use a loop as another option. I ended up using a while loop and that is getting me what I want. Since I want to improve and be an efficient R user, I would like to know if there were other techniques or packages for solving this. My while loop works fine so I don't need help with getting it to work, although I am open to how to improve it.

Thanks in advance for your comments

Here is an example of what I have where x is some number:

numbers = [x1 x2.....x50]

New dataframe that I want to create:

  |      | col1  |col2 | ...  | col5 |
  |:---- |:-----:|:---:|:----:|-----:|
  |row 1 |  x1   | x2  | ...  |  x5  |
  |:-----|:-----:|:---:|:----:|-----:|
  | row y|  ...  | ... | ...  |  ... |         
  |:-----|:-----:|:---:|:----:|-----:|
  |row 10| x45   | x46 | ...  |  x50 |

My while loop:

i=1
j=1 (for keeping track of columns)
k=1 (for keeping track of rows)

while (i < length(numbers)+1) { 
    if (j > 5)  {
      j = 1
      k=k+1
    } else {
        df[k,j] <- numbers[i]
        i= i +1
        j= j+ 1
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source