bodega18
bodega18

Reputation: 654

Add ID to each Row of DataFrame in groups of 2 - R

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

Answers (1)

akrun
akrun

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

Related Questions