Will Oldham
Will Oldham

Reputation: 1054

Identify which row of data.frame exactly matches a vector

Given this data.frame:

var1 <- c(1, 2)
var2 <- c(3, 4)
var3 <- c(5, 6)
df <- expand.grid(var1 = var1, var2 = var2, var3 = var3)

  var1 var2 var3
1    1    3    5
2    2    3    5
3    1    4    5
4    2    4    5
5    1    3    6
6    2    3    6
7    1    4    6
8    2    4    6

I would like to identify the data.frame row number matching this vector (4 is the answer in this case):

vec <- c(var1 = 2, var2 = 4, var3 = 5)

var1 var2 var3 
   2    4    5 

I can't seem to sort out a simple subsetting method. The best I have been able to come up with is the following:

working <- apply(df, 2, match, vec)
which(apply(working, 1, anyNA) == FALSE)

This seems less straightforward than expected; I was wondering if there was a more straightforward solution?

Upvotes: 1

Views: 785

Answers (4)

Uwe
Uwe

Reputation: 42544

For the sake of completeness, subsetting can be implemented using data.table's join:

library(data.table)
setDT(df)[as.list(vec), on = names(vec), which = TRUE]
[1] 4

Upvotes: 2

mysteRious
mysteRious

Reputation: 4294

Here is a dplyr option:

library(dplyr)
library(magrittr)

df %>% mutate(new=paste0(var1,var2,var3), num=row_number()) %>% 
    filter(new=="245") %>% select(num) %>% as.integer()

[1] 4

Upvotes: 1

Ronak Shah
Ronak Shah

Reputation: 388962

We can transpose the dataframe, compare it with vec and select the row where all of the value matches.

which(colSums(t(df) == vec) == ncol(df))
#[1] 4

Upvotes: 3

NM_
NM_

Reputation: 1999

This can be solved using the prodlim library:

> library(prodlim)
> row.match(vec, df)
[1] 4

Upvotes: 2

Related Questions