r - check number of times consecutive value appear based on a certain criteria -
I have to find out that numbers from 2/3/4/5/6 to any number are in more than one number Appears vector For example, for the following vector
x = c (1,1,3,4,9,1,9,1,5,4,5,2,1,1,4 , 6) There are two consecutive numbers & gt; 3 (49) & amp; (4,6) = 2 consecutive number three & gt;
The following code only gives me how many times a value becomes constant <3> (3, such as 5, 4, 5) = 1 and so on for 4,5,6 consecutive numbers
Pre> runs & lt; - with rle (x) (run, table (value, length)) I really appreciate any help.
Thanks
Try:
F1 & lt; - (VCC, VAL, N) {RL & LT; - RL (VCACT) Val (RL $ length [RL $ value] == n)} F1 (x, 3, 2) # [1] 2F1 (x, 3, 3) # [1] 1 or to do this at once
sapply (2: 6, function (y) {rl> lt; - rl (x> y ); Yoga (RL $ length [RL $ value] == 2)}) # [1] 1 2 0 0 supplementary (2: 6, function (y) {rl & lt; - rle (x> y); zodiac (Rl $ length [rl $ value] == 3)} # [1] 2 1 0 sapply (2: 6, function (y) {rl & lt; -rle (x> y); yoga ( RL $ length [RL $ value] == 4)}) # [1] 0 0 0 0 0
Comments
Post a Comment