Reputation: 654
Simple question - but I need to add an ID column for each row in a dataframe, in groups of 2. The first 2 rows of the dataframe should have ID of 1, 3rd and 4th row of the data should have ID of 2, etc.
Upvotes: 0
Views: 77
Reputation: 886948
It can be done with gl
in base R
. Specify the number of rows and the k
as 2
df1$ID <- as.integer(gl(nrow(df1), 2, nrow(df1)))
Upvotes: 1