0

例如:

Data1:
      customer_id  product sales
1           1       mixer     3
2           2       mixer     3
3           3       mixer     3
4           4       toaster   3
5           5       toaster   2
6           6       lcd tv    2
7           7       lcd tv    1
8           8       lcd tv    1


Data2:

    customer_id       No_of_products_ordered    response
1           2                    two             positive
2           4                    two             positive
3           6                    two             negative
4           8                  three             positive
5          10                  three             positive
6          12                   five             negative
7          14                   five             negative

输出应该是:

       customer_id  product sales

1           1       mixer     3
3           3       mixer     3
5           5       toaster   2
7           7       lcd tv    1

也就是说,它从 data1 中删除了那些也在 data 2 中的 id。

4

3 回答 3

1

当然这是重复的,但答案是:

Data1[!Data1$customer_id %in% Data2$customer_id,]
于 2013-08-07T09:01:16.637 回答
0

这是通过使用 sqldf:

data5 <- sqldf("select a.customer_id , a.name
from Data1 a
left join Data2 b
on a.customer_id = b.customer_id
where b.No_of_products_ordered 为空")

于 2013-08-12T10:25:42.993 回答
0

我尝试了各种方法并得出了这个答案(通过使用合并):
data3 <- merge(x=Data1,y=Data2,by="customer_id",all.x=TRUE)
data4 <- data3[is.na(data3 $No_of_products_ordered),]

给出以下输出:

customer_id    name            product   sales      No_of_products_ordered  response
       1       Rahul Ranjan    mixer     3                   <NA>           <NA>
       3       P Paul          mixer     3                   <NA>           <NA>
       5       Sunny Tiwari    toaster   2                   <NA>           <NA>
       7       Ambreen Khan    lcd tv    1                   <NA>           <NA>
于 2013-08-12T10:02:39.053 回答