Reputation: 1329
So here is my table named qb
:
GameID Qtr Drive Down Passer
1 1 1 1 A
1 1 2 1 A
1 1 2 2 A
1 1 2 3 A
and here is second table named sacks
which I want to be used for filtering
gameId Qtr Drive Down Sack
1 1 2 2 yes
1 1 2 3 yes
I try to filter rows of QB table where gameID, Drive and Down are same as in sacks table.
GameID Qtr Drive Down Passer
1 1 2 2 A
1 1 2 3 A
I tried dplyr
and qb %>% filter(Drive == sacks$Drive & Down==sacks$Down & gameID==sacks$gameID)
but it doesn't work. I could use merge but looking for some "filter solution" (my example is simplier then what I'm trying to handle). Also I'm receiving weird warning
In addition: Warning messages: 1: In c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, : longer object length is not a multiple of shorter object length 2: In c(NA, 1, 1, 2, 1, 1, 1, 2, 2, 3, 4, 1, 1, 1, 1, 2, 3, 4, 1, 2, : longer object length is not a multiple of shorter object length
EDIT: I think I over-complicated my question. I will try once again. I have table qb like in example and second table where I have 2 columns: gameID and Drive. As my final result I want to see all rows where GameID = gameID and Drive=Drive
Upvotes: 0
Views: 138
Reputation: 43344
dplyr::semi_join
is built for this purpose:
library(dplyr)
semi_join(qb, sacks %>% rename(GameID = gameId))
## GameID Qtr Drive Down Passer
## 1 1 1 2 2 A
## 2 1 1 2 3 A
You could use the by
parameter instead of rename
, but that's more typing in this case.
Upvotes: 1
Reputation: 24188
You can use basic subsetting:
qb[qb$GameID == sacks$gameId &
qb$Drive == sacks$Drive &
qb$Down == sacks$Down,]
# GameID Qtr Drive Down Passer
#3 1 1 2 2 A
#4 1 1 2 3 A
Upvotes: 1