Create a subsetting function according to one or more couple of values for a data.frame

How to make a function to use one or mores couples of values (x1,y1 ; x2,y2 ; ... according to need) to subset a data frame like

selection <- function(x1,y1, ...){
               dfselected    <- subset(df, V1 == "x1" & V2 == "y1" 
              ##  MAY OR MAY NOT BE PRESENT ##
                                         | V1 == "x2" & V2 == "y2")
               return(dfselected)
                                  }

I can do it with subset() for a single indexing. Example:

df <- data.frame(
        V1 = c(rep("a",5), rep("b",5)),
        V2 = rep(c(1:5),2),
        V3 = c(101:110)
                 )

ie

V1 V2  V3
a  1  101
a  2  102
a  3  103
a  4  104
a  5  105
b  1  106
b  2  107
b  3  108
b  4  109
b  5  110

And the subsetting for the couples ("a","3") and ("b","4") look likes

dfselected <- subset(df, V1 == "a" & V2 == 3 | V1 == "b" & V2 == 4 )

I couldn't find a similar function. I don't know if I have to pass an unspecified number of parameters to a function (the so-called "three dots") or to use if/else. I'am a beginner to functions, so links or examples are welcome too. I started mostly with that: http://www.ats.ucla.edu/stat/r/library/intro_function.htm

------------------------------ Solution after hadley's answer

selection <- function (x,y){
                            match <- data.frame(
                                               V1 = x,
                                               V2 = y,
                                               stringsAsFactors = FALSE
                                                )
                            return(dplyr::semi_join(df, match))
                           }

Answers


It sounds like you want a semi-join: find all rows in x that have matching entries in y:

df <- data.frame(
  V1 = c(rep("a",5), rep("b",5)),
  V2 = rep(c(1:5), 2),
  V3 = c(101:110),
  stringsAsFactors = FALSE
)

match <- data.frame(
  V1 = c("a", "b"),
  V2 = c(3L, 4L),
  stringsAsFactors = FALSE
)

library(dplyr)
semi_join(df, match)

Unless I'm missing something, you could just use base R's merge().

With the two example data.frames Hadley provided,

merge(df, match)
#   V1 V2  V3
# 1  a  3 103
# 2  b  4 109

Need Your Help