如何检查我的 table 是否符合另一个 table 中的条件

How to check if my table matches criteria in another table

我是 R 的新手。这个问题似乎很常见,但我无法从以前的问题中找到相关答案。

我有如下数据:

我的最大限制如下表(每一行都是一个单独的标准)。

我想比较我的数据中符合这些条件的所有行,并希望将黄色列作为结果 return。

希望这是清楚的。

我们可以用outer得到与函数大于,(>)比较的所有值组合。我们对两列都这样做并将它们加在一起。我们正在寻找两列都超过限制,所以基本上寻找 2 的总和。一旦我们有了它,我们就可以使用 rowSums 来获取至少有 1 个非零的行,即

m1 <- (outer(df$column1, df1$Forcolumn1, `>`) + outer(df$column2, df1$Forcolumn2, `>`) == 2) * 1

#     [,1] [,2] [,3] [,4] [,5]
#[1,]    0    0    0    0    0
#[2,]    0    1    0    0    0
#[3,]    0    0    0    0    0
#[4,]    0    0    1    0    0
#[5,]    0    0    1    0    0
#[6,]    0    0    0    0    1

使用 rowSums 我们得到您预期的输出,

rowSums(m1 > 0)
#[1] 0 1 0 1 1 1

数据

dput(df)
structure(list(Data = 1:6, column1 = 11:16, column2 = c(3, 3, 
2, 2, 1, 0)), class = "data.frame", row.names = c(NA, -6L))

dput(df1)
structure(list(max_limit = 1:5, Forcolumn1 = c(11, 11, 13, 14, 
15), Forcolumn2 = c(3, 2, 0, 1, -1)), class = "data.frame", row.names = c(NA, 
-5L))