Suppose I have 5 colors - I can sort these colors 5! different ways:
library(combinat)
library(dplyr)
my_list = c("Red", "Blue", "Green", "Yellow", "Orange")
d = permn(my_list)
all_combinations = as.data.frame(matrix(unlist(d), ncol = 120)) %>%
setNames(paste0("col", 1:120))
all_combinations[,1:5]
col1 col2 col3 col4 col5
1 Red Red Red Red Orange
2 Blue Blue Blue Orange Red
3 Green Green Orange Blue Blue
4 Yellow Orange Green Green Green
5 Orange Yellow Yellow Yellow Yellow
I now want to remove all columns from the above data set where ::
We can reformulate this problem to a simpler one by transposing the data frame and instead filtering the rows that apply to your condition. Then transpose again and you have your result.
library(combinat)
library(dplyr)
my_list = c("Red", "Blue", "Green", "Yellow", "Orange")
d = permn(my_list)
all_combinations = as.data.frame(matrix(unlist(d), ncol = 120)) %>%
setNames(paste0("col", 1:120))
all_combinations %>%
t() %>% # first transpose returns a matrix so
as.data.frame() %>% # convert to data.frame
filter(!V5 == "Yellow",
V1 == "Red") %>% # filter out the rows as by your conditions
t() %>% # transpose again
as.data.frame() # convert again
#> col1 col7 col8 col9 col10 col11 col12 col13 col14 col27 col28
#> V1 Red Red Red Red Red Red Red Red Red Red Red
#> V2 Blue Orange Blue Blue Blue Yellow Yellow Yellow Orange Orange Yellow
#> V3 Green Blue Orange Yellow Yellow Blue Blue Orange Yellow Yellow Orange
#> V4 Yellow Yellow Yellow Orange Green Green Orange Blue Blue Green Green
#> V5 Orange Green Green Green Orange Orange Green Green Green Blue Blue
#> col29 col30 col31 col32 col33 col34 col40
#> V1 Red Red Red Red Red Red Red
#> V2 Yellow Yellow Green Green Green Orange Green
#> V3 Green Green Yellow Yellow Orange Green Blue
#> V4 Orange Blue Blue Orange Yellow Yellow Yellow
#> V5 Blue Orange Orange Blue Blue Blue Orange