dplyr filter : value is contained in a vector

后端 未结 1 1829
醉梦人生
醉梦人生 2021-01-03 22:10

Is there a straight way to filter using dplyr::filter given an specific vector?

I\'m looking for something like the code below:

top.food         


        
1条回答
  •  孤街浪徒
    2021-01-03 22:48

    I think you are looking for the value matching function %in%.

    filter(orders, dish %in% top.food)
    

    Or you can switch to slice() and use match().

    slice(orders, match(dish, top.food, 0L))
    

    I believe that slice() is a bit faster than filter(), so that might be beneficial to you. See help(match) for all the details on value matching.

    0 讨论(0)
提交回复
热议问题