Subset data mapping two variables in another data frame

Situation

I have two dataframes, df1

and df2

:

id <- c(1, 2, 3, 4, 5, 6, 7, 8)
position <- c("AA", "BB", "CC", "DD", "EE", "FF", "GG", "HH")
value <- c(100, 200, 300, 400, 500, 600, 700, 800)
df1 <- data.frame(id, position, value)

id <- c(1, 2, 3, 4, 6, 8)
position <- c("AA", "BB", "CC", "EE", "DD", "HH")
value <- c(700, 800, 900, 100, 200, 900)
df2 <- data.frame(id, position, value)

      

Question

I would like to select all lines in df1

where there is a matching id

AND position

in df2

.

Attempts

I'm sure it's pretty straightforward, but I'm trying my best to find a suitable solution.

I can do this with merge

, but I would like to avoid hard-coded column and variable names deletion.

#this method works for this small example but I have many variables and don't want
#to write them all out when removing the unwanted ones
df3 <- merge(x=df1, y=df2, by=c("id","position"))
df3 <- df3[,-4]    #remove unused column
df3

      

I thought I could use dplyr

, something like

#library(dplyr)
df3 <- filter(df1, id %in% df2$id) 
df3

      

But I don't have much luck overlap on two variables.

Desired output

  id position value.x
1  1       AA     100
2  2       BB     200
3  3       CC     300
4  8       HH     800

      

Any help was appreciated.

+3


source to share


2 answers


merge

could be used quite efficiently without having to drop columns after the merge.



keys <- c("id","position")
merge(df1,df2[keys],by=keys)

#  id position value
#1  1       AA   100
#2  2       BB   200
#3  3       CC   300
#4  8       HH   800

      

+3


source


You can paste

combine two columns and match

these:



    df1[match(paste(df2$id,df2$position),paste(df1$id,df1$position),nomatch=0),]
    #id position value
    #1  1       AA   100
    #2  2       BB   200
    #3  3       CC   300
    #8  8       HH   800

      

+1


source







All Articles