Identify similar repeating ascending patterns in numeric vectors in R -
i have multiple numeric vectors have similar repeating ascending pattern. eg:
vec_1 <- c(43, 17, 186, 193, 186, 186, 474, 491, 498, 498, 673, 736, 743, 716, 44, 19, 193, 194, 193, 193, 472, 498, 476, 499, 673, 743, 714, 714, 19, 21, 194, 180, 194, 194, 485, 499, 481, 476, 712, 719, 712, 17, 40, 174, 180, 169, 495, 485, 673, 177, 485, 481, 714, 730, 733, 40, 33, 190, 174, 180, 482, 495, 495, 479, 703, 733, 704)
there 5 repetitions. in above example:
- starts 43, ends 716
- starts 44 (where 1. ends), ends 714
- etc.
i want generate new vector identifies repetition number. vec_1 be:
rep_nums_1 <- c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, ..., 5)
the number of items within each repetition can differ (so cant assign first 14 elements 1, next 14 2, etc.)
not sure how best approach this. non elegant solution check if next element in sequence smaller current more e.g. 300 , change group number if so. better suggestions? thank you.
you go more elaboration on whence "pattern", think trick:
rep(1:5, diff(c(0, which(diff(vec_1) < -500), length(vec_1))))
there should approach rle
well.
Comments
Post a Comment