Consider the sample data
df <-
  structure(
    list(
      id = c(1L, 1L, 1L, 1L, 2L, 2L, 3L),
      A = c(20L, 12L, 13L, 8L, 11L, 21L, 17L),
      B = c(1L, 1L, 0L, 0L, 1L, 0L, 0L)
    ),
    .Names = c("id", "A", "B"),
    class = "data.frame",
    row.names = c(NA,-7L)
  )
Each id (stored in column 1) has varying number of entries for column A and B. In the example data, there are four observations with id = 1.  I am looking for a way to subset this data in R so that there will be at most 3 entries for for each id and finally create another column (labelled as C) which consists of the order of each id. The expected output would look like:
df <-
  structure(
    list(
      id = c(1L, 1L, 1L, 2L, 2L, 3L),
      A = c(20L, 12L, 13L, 11L, 21L, 17L),
      B = c(1L, 1L, 0L, 1L, 0L, 0L),
      C = c(1L, 2L, 3L, 1L, 2L, 1L)
    ),
    .Names = c("id", "A", "B","C"),
    class = "data.frame",
    row.names = c(NA,-6L)
  )
Your help is much appreciated.
 
     
     
    