rsequencesequential-number

Number of occurrences of next value in a vector in R


I have a vector with random numbers from 1-7 and I want to be able to determine the next number in the vector that follows the first value so that I can look for patterns and get a 7x7 matrix of values that shows how often a specific number follows another.

First number in rows and count of next number in columns.

I am trying to do this with a double loop currently, but am getting confused in the process.

For example-

Example vector-

floor(runif(10,1,8))
[1] 3 5 6 6 6 1 7 7 5 3

Answer-

  1  2  3  4  5  6  7
1 0  0  0  0  0  0  1
2 NA NA NA NA NA NA NA
3 0  0  0  0  1  0  0
4 NA NA NA NA NA NA NA
5 0  0  1  0  0  1  0
6 1  0  0  0  0  3  0
7 0  0  0  0  1  0  1

Solution

  • Given a vector

    v <- c(3, 5, 6, 6, 6, 1, 7, 7, 5, 3)
    

    if you call table() on the vector you get a count of each element

    table( v )
    # v
    # 1 3 5 6 7 
    # 1 2 2 3 2
    

    If you call table() on mulitple vectors, you get a count for each combination of values.

    Since you want to 'count' the number of times a value appears with the value next to it, you can call table() on the original vector, and the same vector shifted one-value to the right.

    table( v[1:(length(v)-1)], v[2:length(v)] )
    
    #  y
    # x  1 3 5 6 7
    #  1 0 0 0 0 1
    #  3 0 0 1 0 0
    #  5 0 1 0 1 0
    #  6 1 0 0 2 0
    #  7 0 0 1 0 1
    

    Which can be made more succinct (courtesy of Julius Vainora ) by

    table(head(v, -1), tail(v, -1))
    

    Note: the result doesn't include the NA rows you've got in your question.