简体   繁体   English

需要计算R中向量中特定转换的数量

[英]need to count number of specific transitions in a vector in R

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. 我在R中编写一个采样器,它基本上是一个很大的for循环,对于每个迭代我都要计算一个向量中的转换数。 I have a vector called k, which contains zeros and ones, with 1000 entries in the vector. 我有一个名为k的向量,它包含0和1,向量中有1000个条目。

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. 因此,每次循环运行时,变量n00,n01,n10,n11计算向量中的转换。 For example, n00 counts number of times a 0 is followed by another 0. And so on... 例如,n00计算0后跟另一个0的次数。依此类推......

This is very slow, and I am very new to R, so I am kind of desperate here. 这很慢,我对R很新,所以我在这里绝望。 I do not understand how to use grep, if that even is possible. 我不明白如何使用grep,如果可能的话。

Thank you for your help 谢谢您的帮助

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. 向量xheadtail返回部分:除了最后一个元素之外的所有部分,然后是除第一个元素之外的所有元素。 This means that the corresponding elements are the consecutive pairs from x . 这意味着相应的元素是来自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. 然后paste0只是将每一个转换为一个字符向量并粘贴第一个元素,第二个元素等。结果是一个字符向量,其元素如“00”,“01”等。然后table只计算每个元素的数量有。

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. 只运行head(x,-1)等,看看每件作品的作用。

To address the comment below, to ensure that all types appear with counts when you run table , convert it to a factor first: 要解决以下注释,要确保在运行table时所有类型都显示计数,请先将其转换为系数:

x1 <- factor(paste0(head(x,-1),tail(x,-1)),levels = c('00','01','10','11'))
table(x1)
x <- sample(0:1, 10, replace = TRUE)
# my sample: [1] 0 0 0 0 0 1 0 1 1 0

rl <- rle(x)

zero_to_zero <- sum(rl$len[rl$val == 0 & rl$len > 1] - 1)
one_to_one <- sum(rl$len[rl$val == 1 & rl$len > 1] - 1)

zero_to_one <- sum(diff(rl$val) == -1)
one_to_zero <- sum(diff(rl$val) == 1)

x
# [1] 0 0 0 0 0 1 0 1 1 0
zero_to_zero
# [1] 4

one_to_one
# [1] 1

zero_to_one
# [1] 2

one_to_zero
# [1] 2

@joran's answer is faaaar cleaner though...Still, I thought I just as well could finish the stroll I started down (the dirty) trail, and share the result. @joran的回答是faaaar清洁虽然......但是,我想我也可以完成我开始下来(肮脏的)路径的漫步,并分享结果。

If we don't care about distinguishing the n00 and n11 cases, then this becomes much simpler: 如果我们不关心区分n00和n11的情况,那么这变得更加简单:

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. 由于问题说你主要对过渡感兴趣,这可能是可以接受的,否则其中一个答案会更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM