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 ::
- the 5th row does not equal "yellow" OR
- the 1st row equals "red"
I tried to do this:
cond <- all_combinations[which(all_combinations[1,] == "Red" || all_combinations[5,] != "Yellow" ), ]
But the above code is returning a data frame with an incorrect number of dimensions.
Can someone please show me how to fix this problem?
Thanks!