简体   繁体   中英

Using dplyr to subset a data based on common values in one column from two data frames

I have never really used dplyr and wondering how I can use it in the following context. So, I have following two data frames:

 trainData <- read.csv("train.csv", header = TRUE, stringsAsFactors = FALSE)
 subscriptionData <- read.csv("subscriptions.csv", header = TRUE, stringsAsFactors = FALSE)
> head(trainData)
       account.id total
1 001i000000NuOGY     0
2 001i000000NuS8r     0
3 001i000000NuPGw     0
4 001i000000NuO7a     0
5 001i000000NuQ2f     0
6 001i000000NuOQz     0

> head(subscriptionData)
       account.id    season package no.seats          location           section price.level total multiple.subs
1 001i000000LhyR3 2009-2010 Quartet        2     San Francisco Premium Orchestra           1   1.0            no
2 001i000000NuOeY 2000-2001    Full        2     San Francisco         Orchestra           2   2.0            no
3 001i000000NuNvb 2001-2002    Full        2 Berkeley Saturday     Balcony Front           3   2.0            no
4 001i000000NuOIz 1993-1994 Quartet        1      Contra Costa         Orchestra           2   0.5            no
5 001i000000NuNVE 1998-1999    Full        2   Berkeley Sunday      Balcony Rear           4   2.0            no

Now I want to take subset of subscriptionData based on the account.id of trainData . I basically want to take subset of subscriptionData with account.id that are also present in trainData .

I know it's a very basic question but I am totally new dplyr and have no clue.

您想要半连接:

subscriptionData %>% semi_join(trainData, by = "account.id")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM