首页 > 解决方案 > 如何在一个数据框中找到唯一值并使用它来过滤 R 中的另一个数据框

问题描述

我有两个引用相同实验的数据框(bmdat1、plots1)。我需要从 plots1$Bucket 中找到唯一值,并使用它们通过 plots1#Bucket 中的唯一值过滤第二个数据框。

这是我尝试过的

bm1dat1 <- filter(bmdat1, bucket == as.vector(unique(plots1$Bucket)))

这返回

Warning messages:
1: In `==.default`(bucket, as.vector(unique(plots1$Bucket))) :
  longer object length is not a multiple of shorter object length
2: In is.na(e1) | is.na(e2) :
  longer object length is not a multiple of shorter object length

我也试过

bm1dat1 <- filter(bmdat1, bucket == unique(plots1$Bucket))

我收到了

Error in Ops.factor(bucket, unique(plots1$Bucket)) : 
  level sets of factors are different

标签: rfilter

解决方案


我认为您需要使用%in%来检查左侧集合中的元素是否存在于右侧集合中(这听起来像是您正在尝试做的事情)。这应该可行,但不知道,因为我们没有您的数据样本可供使用。

bm1dat1 <- filter(bmdat1, bucket %in% unique(plots1$Bucket))

推荐阅读