James Marshall
James Marshall

Reputation: 91

filtering multiple columns of a dataframe by multiple conditions on the same column

I have a data frame mydf and I aggregated mydf by item_i class_i and dept_i. This is the output after aggregation where count is how many observations of item_i class_i and dept_i are in mydf. Then I set a count threshold and delete the rows that count < 5. What I want now is to obtain from mydf all the observation that are in the following data frame. For example all the item_i == -1 and class_i == 0 and dept_i == 210, etc etc. Any suggestions?

   item_i class_i dept_i count
1      -1       0    210    30
4      57       0    210     6
10    129       0    210     8
11    130       0    210     9
13    132       0    210     9
28    248       0    210     6

Upvotes: 0

Views: 114

Answers (1)

Sotos
Sotos

Reputation: 51592

Using dplyr package, then simply

semi_join(mydf, newdf)
#item_i class_i dept_i count
#1     -1       0    210    30
#2     57       0    210     6
#3    129       0    210     8

Data

mydf <- structure(list(item_i = c(-1L, 57L, 129L, 130L, 132L, 248L), 
    class_i = c(0L, 0L, 0L, 0L, 0L, 0L), dept_i = c(210L, 210L, 
    210L, 210L, 210L, 210L), count = c(30L, 6L, 8L, 9L, 9L, 6L
    )), .Names = c("item_i", "class_i", "dept_i", "count"), class = "data.frame", row.names = c("1", 
"4", "10", "11", "13", "28"))
newdf <- structure(list(item_i = c(-1L, 57L, 129L), class_i = c(0L, 0L, 
0L), dept_i = c(210L, 210L, 210L), count = c(30L, 6L, 8L)), .Names = c("item_i", 
"class_i", "dept_i", "count"), row.names = c("1", "4", "10"), class = "data.frame")

Upvotes: 1

Related Questions