dplyr filtering on multiple columns using “%in%”

后端 未结 2 1192
悲&欢浪女
悲&欢浪女 2021-01-29 03:43

I have a dataframe (df1) with multiple columns (ID, Number, Location, Field, Weight). I also have another dataframe (df2) with more information (ID, PassRate, Number, Weight). <

2条回答
  •  说谎
    说谎 (楼主)
    2021-01-29 04:12

    From the question and sample code, it is unclear whether you want df_sub to contain the rows in df1 which do have matches in df2, or the ones without matches. dplyr::semi_join() will return the rows with matches, dplyr::anti_join() will return the rows without matches.

    df_sub <- semi_join(x=df1, y=df2, by=c("ID","Weight")) 
    

    or

    df_sub <- anti_join(x=df1, y=df2, by=c("ID","Weight")) 
    

提交回复
热议问题