I have a problem to find patterns in this data. My data looks like shown here.
df <- read.table(text="16000 1
16000 2
16000 3
16000 5
10000 6
10000 9
10000 12
10000 12
13000 2
14000 4",h=F,strin=F, col.names = c("Amount", "Month"))
In this case I would like to find patterns in the payments. So same amount and consecutive months:
16000 1
16000 2
16000 3
This is a pattern that I look for. The payment in month=4 is not the same amount and the payment in month =5 does not belong to the pattern, because there was no payment in month =4. (Minimum 3 consecutive months with equal amounts.)
Other possible patterns are every three months. In the posted data:
10000 6
10000 9
10000 12
EDIT: I made a really bad solution looking for a better one.
df<-df[order(df$Amount),]
df$pattern <- FALSE
for (i in 2:nrow(df)){
if (df[i,1] == df[i -1 ,1] & df[i,1] == df[i +1 ,1]){
if(df[i,2] == df[i-1,2]+1 & df[i,2] == df[i+1, 2]-1 ){
df[i-1,3]<-TRUE
df[i,3]<-TRUE
df[i +1,3]<-TRUE
}
}
}
This solution is bad because it is very slow and has a small mistake. It does not find the pattern in this situation, because the third time a value appears ordering does not work anymore. In addition to that the solution has problems to find the pattern for the last value of the data.
df <- read.table(text="16000 1
16000 2
16000 3
16000 3
16000 5
10000 6
10000 9
10000 12
10000 12
13000 2
14000
4",h=F,strin=F, col.names = c("Amount", "Month"))
@Moody_Mudskipper The expected Output can be a TRUE/False oder even better a distinct number for each pattern. If a row appears more then ones it is not part of the pattern. I want a marker/column for each pattern.