简体   繁体   English

如何计算 R 中向量中有序序列的数量

[英]How to count the number of ordered sequence in a vector in R

I have the following data set as an example.我以以下数据集为例。

Data<-c("a","d","c","b","d","a","d","b","c","b","a","b","d","c","b","a","c","d","d","c")

I need to find out how many times a < b < c < d, all possible combinations.我需要找出 a < b < c < d 的次数,所有可能的组合。

I have created a loop (using 4 IF functions) but it takes long time when we have large data set.我创建了一个循环(使用 4 个 IF 函数),但是当我们拥有大量数据集时需要很长时间。 With this loop the answer is 47 times.有了这个循环,答案是 47 次。

Is there any efficient way to do so in R.在 R 中是否有任何有效的方法可以做到这一点。

Here is my attempt, but it is very slow when say we have more than 4 letters and large data set.这是我的尝试,但是当我们有超过 4 个字母和大型数据集时,它非常慢。


Data<-c("a","d","c","b","d","a","d","b","c","b","a","b","d","c","b","a","c","d","d","c")
set.seed(123)
Data0<-sort(sample(1:100,20))

df<-data.frame(Data,Data0)

A<-df[Data=="a",2]
B<-df[Data=="b",2]
C<-df[Data=="c",2]
D<-df[Data=="d",2]


myfun<-function(A,B,C,D){
b0<-0
f0<-0
for (i in 1:length(A)){
for (j in 1:length(B)){
for (k in 1:length(C)){
for (l in 1:length(D)){

f0<-f0+sum(A[i]<B[j])*sum(B[j]<C[k])*sum(C[k]<D[l])

}}}}

return(f0)
}


myfun(A,B,C,D)

Many Thanks非常感谢

You could use split to divide Data0 based on groups ie Data , create all possible combinations using expand.grid and count number of times a < b < c < d .您可以使用split根据组(即Data )划分expand.grid Data0所有可能的组合并计算a < b < c < d的次数。

temp <- do.call(expand.grid, split(df$Data0, df$Data))
sum(with(temp, a < b & b < c & c < d))
#[1] 47

If there are many columns and we do not want to check it manually, we can use apply and for every row check the difference between consecutive elements with diff and count number of occurrences when all the values are higher than the previous values.如果有很多列并且我们不想手动检查它,我们可以使用apply并为每一行检查具有diff的连续元素之间的差异,并在all值高于之前的值时计数出现次数。

sum(apply(temp, 1, function(x) all(diff(x) > 0)))
#[1] 47

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

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