使用Regex查找重复的事务

时间:2021-10-22 04:28:51

I'm relatively new to regex but need to build a query that will search through a timeseries and find recurring transactions, ones that are recurring every x number of days.

我对正则表达式相对较新,但需要构建一个查询来搜索时间序列并查找重复的事务,这些事务每隔x天重复一次。

x is predefined

x是预定义的

For example:

例如:

If im looking for a pattern repeating every 9 days

如果我正在寻找每9天重复一次的模式

data1 <- c(10.10,0,0,0,0,0,0,0,10.10,0,0,0,0,0,0,0,10.10,0,0,0,0,0,0,0,10.10)

Output: 10.10

产量:10.10

If im looking for a pattern repeating every 14 days

如果我正在寻找每14天重复一次的模式

data1 <- c(2000,0,0,0,9,0,0,10,0,0,9,0,0,0,0,2000,0,0,0,0,0,0,10.10,0,0,0,10.10,0,0,0,2000,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2000)

Output: 2000

产出:2000

Numbers in between can be anything.

中间的数字可以是任何东西。

1 个解决方案

#1


1  

interval <- 3
vector <- c(10,1,0,10,0,0,10,0,0,10)
for(i in 1:interval) {
  if(sd(vector[seq(i,length(vector),interval)])==0) {
    print(vector[i])
  }
}

This is a loop though, so it won't be the most efficient way of doing things. To be more of a discovery function, returning the value and the interval to get the value, here is a function.

这是一个循环,所以它不是最有效的做事方式。更多的是发现函数,返回值和间隔来获取值,这是一个函数。

vector <- c(10,1,0,10,0,0,10,0,0,10)
matches <- find_patterns(vector,seq(2,3))

find_patterns <- function (vector, intervals) {
    matches <- matrix(c(NA, NA), nrow=1, ncol=2)
    for(interval in intervals) {
        for(i in 1:interval) {
            if(sd(vector[seq(i,length(vector),interval)])==0) {
                if(is.na(matches[1,1])) {
                    matches[1,] <- c(vector[i],interval)
                } else {
                    matches <- rbind(matches,c(vector[i],interval))
                }
            }
        }   
    }
    return(matches)
}

#1


1  

interval <- 3
vector <- c(10,1,0,10,0,0,10,0,0,10)
for(i in 1:interval) {
  if(sd(vector[seq(i,length(vector),interval)])==0) {
    print(vector[i])
  }
}

This is a loop though, so it won't be the most efficient way of doing things. To be more of a discovery function, returning the value and the interval to get the value, here is a function.

这是一个循环,所以它不是最有效的做事方式。更多的是发现函数,返回值和间隔来获取值,这是一个函数。

vector <- c(10,1,0,10,0,0,10,0,0,10)
matches <- find_patterns(vector,seq(2,3))

find_patterns <- function (vector, intervals) {
    matches <- matrix(c(NA, NA), nrow=1, ncol=2)
    for(interval in intervals) {
        for(i in 1:interval) {
            if(sd(vector[seq(i,length(vector),interval)])==0) {
                if(is.na(matches[1,1])) {
                    matches[1,] <- c(vector[i],interval)
                } else {
                    matches <- rbind(matches,c(vector[i],interval))
                }
            }
        }   
    }
    return(matches)
}