Here is my attempted solution. I've tried as much I can to vectorize/matricize hope it's fast enough. Each step is explained in the comment
library(qdapTools)
library(dplyr)
library(data.table)
## generate matrix of attributes
grid_matrix <- do.call(CJ, rep(list(1:0), 5)) %>% as.matrix
attribute_matrix
## att1 att2 att3 att4 att5 att6
## 1 1 1 0 0 1 0
## 2 1 0 0 1 1 0
## 3 0 1 1 1 0 0
## 4 1 1 1 0 0 0
## 5 0 0 0 0 0 1
## create a grid of combination of matrix
grid_matrix <- do.call(CJ, rep(list(1:0), 5)) %>% as.matrix
colnames(grid_matrix) <- paste0("p", 1:5)
## check whether each combination has all attribute presented
combin_all_element_present <- rowSums(grid_matrix %*% attribute_matrix > 0) %>%
`==`(., ncol(attribute_matrix))
combin_all_element_present
## [1] TRUE TRUE TRUE FALSE TRUE TRUE FALSE FALSE TRUE TRUE TRUE
## [12] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [23] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## generate a submatrix which satisfy the condition
grid_matrix_sub <- grid_matrix[combin_all_element_present, ]
## find the combinations with minumun number of p
grid_matrix_sub[rowSums(grid_matrix_sub) == min(rowSums(grid_matrix_sub)), ]
## p1 p2 p3 p4 p5
## [1,] 0 1 0 1 1
## [2,] 0 1 1 0 1
## [3,] 1 0 1 0 1
Note
In case you want to use quanteda, you can generate attribute_matrix
with
library(quanteda)
attribute_matrix <- lapply(list(p1, p2, p3, p4, p5), function(x) paste(x, collapse = ' ')) %>%
unlist %>% tokens %>% dfm %>% as.matrix
attribute_matrix
## features
## docs att1 att5 att2 att4 att3 att6
## text1 1 1 1 0 0 0
## text2 1 1 0 1 0 0
## text3 0 0 1 1 1 0
## text4 1 0 1 0 1 0
## text5 0 0 0 0 0 1