简体   繁体   English

dplyr filter:数据帧中的多个条件

[英]dplyr filter: multiple conditions in a dataframe

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. 我想要的输出是一个包含n数据帧的列表。

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 . 另外,如果可能的话,我想要一个来自cond.df的未知数量列的解决方案。

I am aware that if I only have one variable I can use %in% , eg 我知道如果我只有一个变量,我可以使用%in% ,例如

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

However, I have more than one variable. 但是,我有多个变量。

Thanks 谢谢

I would propose to use an inner_join of mtcars on your cond.df . 我建议在你的mtcars上使用inner_joincond.df This way, it can match on arbitrarily many variables in cond.df . 这样,它可以匹配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

You can use apply to go over your cond.df row-wise, and then use an anonymous function to filter: 您可以使用apply来逐行查看cond.df,然后使用匿名函数进行过滤:

apply(cond.df,1, function(x) mtcars %>% # the 1 is for row wise 
filter(mpg == x[1], gear == x[2], carb == x[3]))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM