Column of sequence with repeated elements based on column with dates [duplicate]

偶尔善良 提交于 2019-12-25 12:48:12

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!