简体   繁体   中英

Filtering a dataframe using exact values in column of different dataframe in R

I have two different data frames. D1 contains all sampling sites with their physical parameters, while D2 contains all animals caught with their respective site ID (a fraction of the sampling sites in D1). I want to filter D1 to only show the sites on D2. In the attached pictures, the column I am interested in is titled "stationkey" on both data frames (numeric values).

I've found ways to filter a column based on <,>,=, etc. However, I have not found any resources on how to filter a data frame by using a set of specific values within a column from a different data frame. Any help would be greatly appreciated.

在此处输入图像描述

在此处输入图像描述

You can try

D1[which(D1[, 'stationkey'] %in% D2[, 'stationkey']), ]

This should get you all of the D1 rows that are also present in D2 based on the stationkey column.

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