Create a data frame from pairs, and then use table :
z <- c(0, 1, 2, 3, 4, 0, 1, 2, 3, 4) pairs <- data.frame(first = head(z, -1), second = tail(z, -1)) table(pairs)
giving:
second first 0 1 2 3 4 0 0 2 0 0 0 1 0 0 2 0 0 2 0 0 0 2 0 3 0 0 0 0 2 4 1 0 0 0 0
or this gives an initial frame of pairs data along with a column of Freq counts:
as.data.frame(table(pairs))
G. grothendieck
source share