Reputation: 7183
I'm sure there's an idiom for doing this, but how exactly do I pipeline a boolean mask or row selection to a filter operation using dplyr.
For example, here I want to select out rows of foo for which id is duplicated:
foo$id %>% duplicated %>% filter(foo ??)
I can do this with an anonymous function, but there must be a better way than:
foo$id %>% duplicated %>% function(x) foo[x,]
Upvotes: 1
Views: 888
Reputation: 206197
The problem is that you are trying to send vector down the pipe when dplyr
is really meant to be used with table-like objects so you should be sending the whole data.frame (assuming that's what foo
is. For example
library(dplyr)
foo <- data.frame(id=sample(1:5, 25, replace=T), val=runif(25))
foo %>% filter(!duplicated(id))
If you really only want the ID's, then add
foo %>% filter(!duplicated(id)) %>% select(id)
Upvotes: 3