Selecting Data Based on "Row" Conditions

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!

Hello there!

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

Created on 2022-01-04 by the reprex package (v2.0.1)

Hope this helps!
Best,
Valentin

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.