I have some vectors like
A1 = c(A,B,C)
A2 = c(A,B,C)
A3 = c(A,B,NA)
A4 = c(NA,B,C)
Now I want something which will give me results like :
Pattern (A,B,C) occurs 2 times.
Pattern (A,B) occurs 3 times.
Pattern (B,C) occurs 3 times.
For now I take each vector and compare them. By this way i can find A,B,C pattern but not A,B or B,C pattern.
Is there any package or some mathematical model which can do it?
EDIT1 : I will not be able to post the code due to some confidentiality issues but essentialy what I did was I compared first vector with second and then to third and so on using %in%. It gave me a matrix of true false. Then I repeated the process for all vectors. Lastly I found out where true have max density in the matrix.
Edit 2 : I know of a-priori algorithm and arules package but a-priori is not very efficient.
A very bad approach (a lot of loops). It is near to what you are looking for.
library(combinat)
A1 = c("A","B","C")
A2 = c("A","B","C")
A3 = c("A","B", NA)
A4 = c(NA,"B","C")
df <- data_frame(A1, A2, A3, A4)
df[is.na(df)] <- " "
a <- sapply(1:dim(df)[1], function(x) {combn(unique(unlist(apply(df, 1, unique))), x)})
pattern <- unlist(lapply(a, function(x){
apply(x, 2, function(y){paste0(y, collapse="_")})
}))
a <- lapply(list(A1, A2, A3, A4), function(x){
x[is.na(x)] <- " "
paste0(x, collapse="_")
})
df2 <- sapply(a, function(x){sapply(pattern, function(z){grepl(z, x)})})
pattern <- rownames(df2)
occurs <- apply(df2, 1, sum)
pattern <- gsub(" ", "NA", pattern)
pattern <- gsub("_", ", ", pattern)
# pattern <- strsplit(pattern, "_")
for(i in 1:length(pattern)){
cat("Pattern (", pattern[[i]], ") occurs ", occurs[i], " times\n")
}