R:如何选择有两个条件的行(买两个产品)

时间:2016-08-16 23:39:02

标签: r data-manipulation

我有一个类似于以下内容的数据集:

 ID = c(1,2,3,4,1,2,3) 
 Product = c("a", "b", "c", "a","b","a","a") 
 Quantity = c(1,1,1,1,1,1,1) 
 df = data.frame(ID, Product, Quantity)

#  ID Product Quantity
#1  1       a        1
#2  2       b        1
#3  3       c        1
#4  4       a        1
#5  1       b        1
#6  2       a        1
#7  3       a        1

我想选择同时购买产品a和产品b的人。在上面的例子中,我想要的结果是:

ID Product Quantity
1  a       1
2  b       1
1  b       1
2  a       1

我无法回想起为我这样做的功能。我能想到的是循环,但我希望找到一个更简洁的解决方案。

4 个答案:

答案 0 :(得分:6)

使用ave

df[
 with(df, ave(as.character(Product), ID, FUN=function(x) all(c("a","b") %in% x) ))=="TRUE",
]
#  ID Product Quantity
#1  1       a        1
#2  2       b        1
#5  1       b        1
#6  2       a        1

答案 1 :(得分:4)

您可以使用dplyr

执行以下操作
library(dplyr)

df %>%
    filter(Product %in% c('a','b')) %>% # Grab only desired products
    group_by(ID)                    %>% # For each ID...
    filter(n() > 1)                 %>% # Only grab IDs where the count >1
    ungroup                             # Remove grouping.

## # A tibble: 4 x 3
##      ID Product Quantity
##   <dbl>  <fctr>    <dbl>
## 1     1       a        1
## 2     2       b        1
## 3     1       b        1
## 4     2       a        1

修改

以下是使用dplyr的更简洁的any版本(类似于Psidom在data.table解决方案中使用它的方式):

df %>% 
    group_by(ID) %>%
    filter(all(c('a','b') %in% as.character(Product))) %>%
    ungroup

答案 2 :(得分:3)

使用data.table的另一个选项:

library(data.table)
setDT(df)[, .SD[all(c("a", "b") %in% Product)], ID]

#   ID Product Quantity
#1:  1       a        1
#2:  1       b        1
#3:  2       b        1
#4:  2       a        1

答案 3 :(得分:1)

以下是使用var getCriteria ={ "stock.subCategoryId": subCategoryId }

的选项
var branch = new Schema({
    branchName: { type: String, trim: true, index: true, default: null },
    isBlocked: { type: Boolean, default: false, required: true },
    email: { type: String, trim: true, required: true, unique: true, index: true },
    password: { type: String, required:true },
    accessToken: { type: String, trim: true, index: true, unique: true, sparse: true },
    stock: [stock],
});

var stock = new Schema({
    categoryId: { type:Schema.ObjectId, ref: "categoies", default: null },
    subCategoryId: { type:Schema.ObjectId, ref: "subCategories", default: null },
    currentPrice: { type: Number },
    isBlocked: { type: Boolean, default: false, required: true },
    Date: { type: Date, default: Date.now, required: true }
});