user722224
user722224

Reputation: 1541

Clever way to match elements in one data frame to elements in another data frame?

Say I have two columns x = c("a", "c", "g") and y = c("a", "b", "c", "d", "e", "f", "g").

x is a column in dataframe1 and y is a column in dataframe 2. dataframe1 does not necessarily match dataframe 2 in dimension.

I want to return the row of y that contains x. So for example, I would like to get 1, 3, 7.

I tried something clumsy like items = which(dataframe1$x == dataframe2$y) but obviously that doesn't work. I know I can do loop through dataframe1$x and match it to dataframe2$y, but it seems like there should be a much better solution.

Upvotes: 0

Views: 3384

Answers (1)

A5C1D2H2I1M1N2O1R2T1
A5C1D2H2I1M1N2O1R2T1

Reputation: 193517

You had the right tool. You just weren't using it correctly. == will match a single value. When you want to match one vector with another, use either match or which with %in% (and not with ==).

Example:

df1 <- data.frame(x = c("a", "c", "g"))
df2 <- data.frame(y = letters[1:7])
match(df1$x, df2$y)
# [1] 1 3 7
which(df2$y %in% df1$x)
# [1] 1 3 7

Upvotes: 1

Related Questions