0

How to force .GRP in data.table to start the group counter from 2 instead of 1?

I have a data.table with groups which I want to sequentially order by group.

example_data.table <- data.table(Var1 = c(1,2,2,4,5,5,5), Var2 = c(1,2,3,7,1,2,3) )

enter image description here

When I use .GRP counter it starts with very first combination as conter 1.

Group_table   <- setDT(example_data.table)[, label := .GRP, by = c("Var1", "Var2" )]

enter image description here

But I want to set group with Var1 value as 4 and Var2 value as 7 to counter value 1 and then the next.

enter image description here

How do I use .GRP in such a way that Var1 as 4 and Var2 as 7 takes a counter as 1 and others in next order?

So, what I was thinking is manually give counter as 1 for the needed combination and for others start the counter from 2 . There are other ways too but I am just a bit confused.

Deb
  • 499
  • 2
  • 15
  • 1
    You need to give a reproducible example https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Kumar Manglam Feb 21 '19 at 14:54
  • @KumarManglam Thanks for the comments! I have updated the qs with a reproducible example. – Deb Feb 21 '19 at 15:15

2 Answers2

3

If you only have one entry with Var1 = 4 & Var2 = 7, then you can remove that entry from .GRP, and use replace to replace it with 1, i.e.

library(data.table)

dt1[-(which(dt1$Var1 == 4 & dt1$Var2 == 7)), Counter := .GRP + 1, by = c('Var1', 'Var2')][, 
                                                Counter := replace(Counter, is.na(Counter), 1)][]

which gives,

   Var1 Var2 Counter
1:    1    1       2
2:    2    2       3
3:    2    3       4
4:    4    7       1
5:    5    1       5
6:    5    2       6
7:    5    3       7
Sotos
  • 51,121
  • 6
  • 32
  • 66
  • Thank you so much! Just one Qs. Why have you used [ ] at last again? Can't we do without it? – Deb Feb 21 '19 at 17:44
  • The brackets at the end are just for displaying the result. You don't have to put them. – Sotos Feb 22 '19 at 05:43
3

If you want certain groups to "start" the count, you can use order to sort during construction:

ex = copy(example_data.table)
ex[order(Var1 != 4, Var2 != 7), g := .GRP, by=.(Var1, Var2)][]

   Var1 Var2 g
1:    1    1 2
2:    2    2 3
3:    2    3 4
4:    4    7 1
5:    5    1 5
6:    5    2 6
7:    5    3 7
Frank
  • 66,179
  • 8
  • 96
  • 180