Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
185 views
in Technique[技术] by (71.8m points)

r - increasing value by one with each occurrence of non-repeated number


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

A base R option with rle

> with(rle(v),rep(seq_along(values),lengths))
 [1] 1 1 2 3 3 3 4 4 5 6 6

or data.table::rleid

> data.table::rleidv(v)
 [1] 1 1 2 3 3 3 4 4 5 6 6

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...