简体   繁体   English

如何检查行是否是data.frame的子集?

[英]How to check if a row is a subset of a data.frame?

Let's say we have this data.frame df 假设我们有这个data.frame df

uid | aid | Freq
-----------------
 2  |  4  |  3
 9  |  1  |  2

How do we check if this row r <- data.frame(uid=9, aid=1) is in df 我们如何检查此行r <- data.frame(uid=9, aid=1)是否在df

For vectors >> we use %in% but it didn't work here. 对于向量>>,我们使用%in%但此处不起作用。

You can check with merge . 您可以使用merge检查。 Just be sure that column names match: 只需确保列名称匹配:

df <- head(mtcars)
r <- data.frame(mpg=18.1, cyl=6)
mrg <- merge(df, r)
#    mpg cyl disp  hp drat   wt  qsec vs am gear carb
# 1 18.1   6  225 105 2.76 3.46 20.22  1  0    3    1

There are many ways you can turn this into a logical test. 您可以通过多种方法将其转变为逻辑测试。

nrow(mrg) > 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM