Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dplyr filter: multiple conditions in a dataframe

Tags:

r

dplyr

I have a data frame which contains all the conditions.

cond.df = data.frame(
  mpg = c(21,18.7,22.8),
  gear = c(4,3,2),
  carb = c(4,3,2)
)

So for my first output, I want a filtered data frame which is equivalent to

mtcars %>% filter(mpg == 21, gear == 4, carb = 4)

My desired output would be a list with n data frames.

list(mtcars %>% filter(mpg == 21, gear == 4, carb = 4),
 mtcars %>% filter(mpg == 18.7, gear == 3, carb = 3),
 mtcars %>% filter(mpg == 22.8, gear == 2, carb = 2))

Also, if possible I want a solution for an unknown number of columns from cond.df.

I am aware that if I only have one variable I can use %in%, e.g.

mtcars %>% filter(gear %in% c(3,4))

However, I have more than one variable.

Thanks

like image 877
chrk623 Avatar asked Apr 17 '26 07:04

chrk623


1 Answers

I would propose to use an inner_join of mtcars on your cond.df. This way, it can match on arbitrarily many variables in cond.df.

I changed your conditions data frame a bit such that the second and third row actually match something.

library(dplyr)
cond.df = data.frame(
    mpg = c(21,18.7,22.8),
    gear = c(4,3,4),
    carb = c(4,2,1)
)

This creates a dataframe with the filtered/joined dataframes in each row.

result <- 
    cond.df %>%
    rowwise() %>%
    do(
        dfs = inner_join(as.data.frame(.), mtcars)
    )

In case you need it as a list of dataframes, just convert it.

as.list(result)$dfs
like image 63
David Avatar answered Apr 19 '26 21:04

David



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!