Your stated objective is to assess whether
the 1s tend to rank lower (i.e. appear earlier)
That is not measured by runs, but by ranks. Use the Wilcoxon (aka Mann-Whitney) test.
This test is both conceptually and computationally simple, yet reasonably powerful. The data are ranked in the order of appearance using the numbers $1, 2, \ldots, n$ (where $n=35$ in this case). The ranks are summed within each group: there's a sum of $n_0=18$ ranks corresponding to the zeros and a sum of $n_1=17$ ranks corresponding to the ones. To compensate for the different numbers of zeros and ones, subtract the smallest possible sum from each (equal to $1+2+\cdots+n_i = \binom{n_i+1}{2}$ for group $i$, $i=0,1$). If the ones truly tend to come first, their adjusted rank sum will be substantially smaller than that of the zeros. This can be converted into a Z-score by assuming an asymptotic Normal distribution for the statistic or a more accurate p-value can be found through the permutation distribution. The code below illustrates both methods.
For these data, the zeros appear at ranks
9 11 17 18 20 21 22 23 24 25 26 27 28 29 30 32 33 35
while the ones appear at ranks
1 2 3 4 5 6 7 8 10 12 13 14 15 16 19 31 34
The adjusted sum of the ranks of the ones is $U=47$. The Normal approximation estimates its p-value at $0.0002339$. The small value is testimony to the power of this test in the present case. The permutation distribution, estimated with a million replications, gives a p-value of $0.000264$. It is accurate to $\pm 0.000016$ (which is one standard error). Either p-value gives you ample basis to reject the null hypothesis that the zeros and ones are randomly scattered throughout the sequence.
Here is a histogram of the permutation distribution of the $U$ statistic for these data.
The red vertical line marks the actual test statistic. It obviously is extreme.
Although it might not look it, this test was conducted as a two-tailed test (by taking the smaller of the two adjusted rank sums). It tests whether there is any difference in ranks, not just whether the ones tend to come earlier.
Below is the (reproducible) R
code that made the figure and computed the p-values. This large simulation takes about ten seconds to run. (Only a few thousand replications are typically needed. A million were used to make the point that the Normal approximation works well in this case.) Reduce the first argument of replicate
at line 25 in order to achieve a faster run time.
x <- c(1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0)
#
# Wilcoxon test.
#
Wilcoxon <- function(x) {
n <- length(x)
n0 <- sum(x==0)
n1 <- sum(x==1)
u0 <- sum((1:n)[x==0]) - choose(n0+1, 2)
u1 <- sum((1:n)[x==1]) - choose(n1+1, 2)
u <- min(u0, u1)
m.u <- n0 * n1 / 2
s.u <- sqrt(n0 * n1 * (n+1) / 12)
Z <- (u - m.u)/s.u
p <- pnorm(Z)
return(c(U=u, Z=Z, p.value=p))
}
stats <- Wilcoxon(x)
#
# Permutation test.
#
set.seed(17)
U <- replicate(1e6, Wilcoxon(sample(x, length(x)))["U"])
hist(U, main="Permutation Distribution", )
abline(v = stats["U"], lwd=2, col="Red")
#
# Summary.
#
message("Normal approximation: ", signif(stats["p.value"], 4),
" Permutation estimate: ", signif(mean(c(1, U <= stats["U"])), 4),
" +/- ", signif(sd(c(1, U <= stats["U"])) / sqrt(1 + length(U)), 2))