Reputation: 185
Here an example of my data.frame:
df = read.table(text = 'colA
1
1
1
0
1
1
0
0
0
1', header = TRUE)
I need to group colA
rows based on specific grouping numbers, such as 3, 2, 2, 3 and assign to them a number from 1900 to 1903.
Desired output:
colA colB
1 1900
1 1900
1 1900
0 1901
1 1901
1 1902
0 1902
0 1903
0 1903
1 1903
The 4 grouping numbers mean: 1st group = first 3 rows, 2nd group = subsequent 2 rows, 3rd group = subsequent 2 rows and 4th group = subsequent 3 rows.
Any suggestion?
Upvotes: 2
Views: 77
Reputation: 886938
We can just use rep
df$colB <- rep(1900:1903, c(3, 2, 2, 3))
df$colB
#[1] 1900 1900 1900 1901 1901 1902 1902 1903 1903 1903
Upvotes: 1