Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

need to count number of specific transitions in a vector in R

Tags:

r

vector

I am programming a sampler in R, which basically is a big for loop, and for every Iterations I have to count the number of transitions in a vector. I have a vector called k, which contains zeros and ones, with 1000 entries in the vector.

I have used the following, horribly slow, code:

#we determine the number of transitions n00,n01,n10,n11
  n00=n01=n10=n11=0 #reset number of transitions between states from last time
  for(j in 1:(1000-1)){
    if(k[j+1]==1 && k[j]==0) {n01<-n01+1}
    else { if(k[j+1]==1 && k[j]==1) {n11<-n11+1}
           else { if(k[j+1]==0 && k[j]==1) {n10<-n10+1}
                  else{n00<-n00+1}
           }

    }
  }

So for every time the loop goes, the variables n00,n01,n10,n11 counts the transitions in the vector. For example, n00 counts number of times a 0 is followed by another 0. And so on...

This is very slow, and I am very new to R, so I am kind of desperate here. I do not understand how to use grep, if that even is possible.

Thank you for your help

like image 687
Helen Avatar asked Jan 12 '23 06:01

Helen


2 Answers

Try something like this:

x <- sample(0:1,20,replace = TRUE)
> table(paste0(head(x,-1),tail(x,-1)))

00 01 10 11 
 4  3  4  8 

The head and tail return portions of the vector x: all but the last element, and then all but the first element. This means that the corresponding elements are the consecutive pairs from x.

Then paste0 just converts each one to a character vector and pastes the first elements, the second element, etc. The result is a character vector with elements like "00", "01", etc. Then table just counts up how many of each there are.

You can assign the result to a new variable like so:

T <- table(paste0(head(x,-1),tail(x,-1)))

Experiment yourself with each piece of the code to see how it works. Run just head(x,-1), etc. to see what each piece does.

To address the comment below, to ensure that all types appear with counts when you run table, convert it to a factor first:

x1 <- factor(paste0(head(x,-1),tail(x,-1)),levels = c('00','01','10','11'))
table(x1)
like image 104
joran Avatar answered Jan 16 '23 00:01

joran


If we don't care about distinguishing the n00 and n11 cases, then this becomes much simpler:

x <- sample(0:1,20,replace = TRUE)
#  [1] 0 0 0 0 0 1 0 1 1 0 0 0 1 0 0 1 0 0 0 0

table(diff(x))      
# -1  0  1 
#  4 11  4 

Since the question says that you're primarily interested in the transitions, this may be acceptable, otherwise one of the other answers would be preferable.

like image 22
user295691 Avatar answered Jan 16 '23 01:01

user295691