Description:
Learn how to create a repeating sequential numbering in R by groups of duplicated values, starting from a specified number, using a concise and efficient vectorized approach.
---
This video is based on the question https://stackoverflow.com/q/79460936/ asked by the user 'denis' ( https://stackoverflow.com/u/11584327/ ) and on the answer https://stackoverflow.com/a/79461005/ provided by the user 'Tim G' ( https://stackoverflow.com/u/28479453/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.
Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: Repeat a sequential numbering of duplicated values starting from a given value in R
Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/licensing
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/by-sa/4.0/ ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/by-sa/4.0/ ) license.
If anything seems off to you, please feel free to drop me a comment under this video.
---
Introduction
When working with grouped data in R, you might want to assign a sequential numbering to each group — but with a twist: the sequence should start from a certain number and repeat cyclically for subsequent groups. This is common when handling categorical or duplicate values that need mapped sequences.
The Problem
You have a variable row1 with repeated values grouped accordingly. You want to create a new variable row2:
For values of row1 less than 3, simply keep the same value.
For values of row1 starting from 3, assign a sequence from 3 to 6 repeatedly.
Even if a group has fewer rows, continue the sequence without breaking.
Example input and desired output:
[[See Video to Reveal this Text or Code Snippet]]
Efficient Solution Using Modular Arithmetic
The key insight is to use the modulo operation to cycle through the sequence from 3 to 6 repeatedly for the groups where row1 >= 3.
Steps:
Subtract 3 from row1 to normalize the sequence starting point.
Take modulo 4 (because you want a cycle length of 4: numbers 3,4,5,6).
Add 3 back to shift the cycle to the desired numbering range.
Keep values less than 3 unchanged.
R code implementation:
[[See Video to Reveal this Text or Code Snippet]]
Output:
[[See Video to Reveal this Text or Code Snippet]]
Why This Works
% (modulo) cycles through 0,1,2,3 for any integer input modulo 4, creating a repeating pattern.
Adding 3 shifts these values to your target sequence (3 to 6).
The if_else condition ensures that values less than 3 remain untouched.
Summary
This approach is concise, fast, and avoids explicit looping:
It uses vectorized operations.
Easily customizable start and cycle length.
Handles incomplete groups gracefully.
You can now apply this pattern whenever you need repeated sequential numbering starting from a custom value by groups of duplicates.
Share this link via
Or copy link























