mabdrabo
mabdrabo

Reputation: 1060

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

Let's say we have this 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

For vectors >> we use %in% but it didn't work here.

Upvotes: 1

Views: 877

Answers (1)

Pierre L
Pierre L

Reputation: 28441

You can check with 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

Upvotes: 4

Related Questions