Let's say I have the following data table:
dt=data.table(type=c('big','medium','small','small' ,'medium','small','small' ,'big','medium','small','small') ,category=letters[1:11]) type category 1: big a 2: medium b 3: small c 4: small d 5: medium e 6: small f 7: small g 8: big h 9: medium i 10: small j 11: small k
In this case, I have a hierarchy of categories: the type "large" is the same for all lines until the next "large" type appears. And the behavior is the same for each type.
Change the form I want should give me the following:
dt=data.table(type=c('big','medium','small','small' ,'medium','small','small' ,'big','medium','small','small') ,category=letters[1:11]) big medium small 1: abc 2: abd 3: aef 4: aeg 5: hij 6: hik
As you can see, each category changes only when a register of the same category is found, the order matters for setting these categories.
Do you think there is a way to do this without using for?