(Update) Add index column to data.frame based on two columns
问题 Example data.frame: df = read.table(text = 'colA colB 2 7 2 7 2 7 2 7 1 7 1 7 1 7 89 5 89 5 89 5 88 5 88 5 70 5 70 5 70 5 69 5 69 5 44 4 44 4 44 4 43 4 42 4 42 4 41 4 41 4 120 1 100 1', header = TRUE) I need to add an index col based on colA and colB where colB shows the exact number of rows to group but it can be duplicated. colB groups rows based on colA and colA -1 . Expected output: colA colB index_col 2 7 1 2 7 1 2 7 1 2 7 1 1 7 1 1 7 1 1 7 1 89 5 2 89 5 2 89 5 2 88 5 2 88 5 2 70 5 3 70