runs macro dollars ) Macro to find lengths of runs of equal values ) Usage: ) result <- runs(x) ) x REAL vector with no MISSING values ) result vector of positive integers with result[i] ) = length of i-th run of equal values ) Example: ) runs(vector(1,1,1,4,3,3,1,1,1,1,5,6,6)) returns ) vector(3,1,2,4,1,2) ) 030305 written by C. Bingham, kb@umn.edu #runlengths <- $S(x) @runs <- @x <- argvalue($1,"sample","nonmissing real vector") @n <- length(@x) for (@i, 1, @n){ @J <- @x == @x[1] if (sum(@J) == @n){ @runs[@i] <- @n break } @runs[@i] <- @j <- min(run(@n)[!@J])-1 @x <- @x[-run(@j)] @n <-- @j ;;} @runs <- @runs[run(@i)] delete(@x,@n,@J,@i,@j,silent:T) delete(@runs,return:T) %runs% runs1 macro dollars ) Macro to find lengths of runs of equal values ) Usage: ) result <- runs(x) ) x REAL vector with no MISSING values ) result vector of positive integers with result[i] ) = length of i-th run of equal values ) Example: ) runs(vector(1,1,1,4,3,3,1,1,1,1,5,6,6)) returns ) vector(3,1,2,4,1,2) ) 030305 written by C. Bingham, kb@umn.edu #runlengths <- $S(x) @x <- argvalue($1,"sample","nonmissing real vector") @runs <- NULL while (T) { @n <- length(@x) @J <- @x == @x[1] if (sum(@J) == @n){ @runs <- vector(@runs,@n) break } @j <- min(run(@n)[!@J])-1 @runs <- vector(@runs,@j) @x <- @x[-run(@j)] ;;} delete(@x,@n,@J,@j,silent:T) delete(@runs,return:T) %runs1%