从一个表中删除行,其中列中的字段与不同表中的同一列的字段匹配

时间:2022-08-21 20:11:29

Hi I have two dataframes (called table1 and table2 respectively):

嗨,我有两个数据帧(分别称为table1和table2):

ID    MONTH
--    -----

1     Jan  
2     May
3     May          
4     Jan


ID     TEST1   GNDR  
--     -----   ----   

1      90      M  
2      80      M  
3      70      F  

where I want to remove from table1 any row where the ID matches the ID in table2, so that I am left with:

我想从table1中删除ID与table2中的ID匹配的任何行,以便我留下:

ID    MONTH
--    -----   

4     Jan      

I just want to say where ID from table2 matches ID in table1, remove all rows relating to those IDs.

我只是想说明table2中的ID与table1中的ID匹配,删除与这些ID相关的所有行。

I can use the merge() function to obtain the rows where ID is common using

我可以使用merge()函数来获取ID常用的行

merge(table1,table2,by="ID") 

and store the results in a dataframe, but I do not know how to delete the rows from table1 based on the result of the merge command.

并将结果存储在数据框中,但我不知道如何根据merge命令的结果从table1中删除行。

Any help would be great.

任何帮助都会很棒。

1 个解决方案

#1


1  

You can use %in%

您可以使用%in%

df1[!df1$ID %in% df2$ID,]
#  ID MONTH
#4  4   Jan

#1


1  

You can use %in%

您可以使用%in%

df1[!df1$ID %in% df2$ID,]
#  ID MONTH
#4  4   Jan