问题
Consider a data frame ordered by a column with dates:
df=data.frame(event=1:12,
subject=rep("M325",12),
date=c(rep("2017-11-01",4),rep("2017-11-14",8)))
What I want is to create a fourth column with a sequence from 1 to the next unique date, with every element in the sequence repeated every i-th date. For example:
event subject date num
1 1 M325 2017-11-01 1
2 2 M325 2017-11-01 1
3 3 M325 2017-11-01 1
4 4 M325 2017-11-01 1
5 5 M325 2017-11-14 2
6 6 M325 2017-11-14 2
7 7 M325 2017-11-14 2
8 8 M325 2017-11-14 2
9 9 M325 2017-11-14 2
10 10 M325 2017-11-14 2
11 11 M325 2017-11-14 2
12 12 M325 2017-11-14 2
Any advice to get this result for n dates will be very appreciated.
回答1:
Despite the answer by @akrun
df$num <-cumsum(!duplicated(df$date))
Or using data.table:
setDT(df)[, num := rleid(date)]
Are faster, this answer using rle can actually solve my problem.
来源:https://stackoverflow.com/questions/47880875/column-of-sequence-with-repeated-elements-based-on-column-with-dates