简体   繁体   中英

How to remove rows from one dataframe based on the column values in a different data frame in R?

I have this dataframe 1

Data frame 2

I would want to remove the entire rows from Data Frame 1 with the batch NO.s in Data frame 2

Final table should look like:

Using anti_join , which basically keeps only the rows in df1 not in df2 :

library(dplpyr)
df1 %>% 
  anti_join(df2, by = "BatchNo.")
# Joining, by = "BatchNo."        # be sure that "BatchNo." is spelled the same
# Month Place BatchNo. Passed
# 1   MAR   CAN    14824      N
# 2   OCT   GER    15842      Y
# 3   JUL   POR    13654      N

Data:

tt <- "Month Place BatchNo. Passed
FEB    NZ     12451    Y
MAR    CAN    14824    N
OCT    GER    15842    Y
JUL    POR    13654    N
MAY    ESP    12445    N"

df1 <- read.table(text=tt, header = T)

tt <- "BatchNo.  Commodity Price
12451        BUS       100
12445        CAR       200"

df2 <- read.table(text=tt, header = T)

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