R: merge two data frames when either of two criteria matches

lll

Say I have two dataframes like the following:

n = c(2, 3, 5, 5, 6, 7) 
s = c("aa", "bb", "cc", "dd", "ee", "ff") 
b = c(2, 4, 5, 4, 3, 2) 
df = data.frame(n, s, b)
#  n  s b
#1 2 aa 2
#2 3 bb 4
#3 5 cc 5  
#4 5 dd 4
#5 6 ee 3
#6 7 ff 2

n2 = c(5, 6, 7, 6) 
s2 = c("aa", "bb", "cc", "ll") 
b2 = c("hh", "nn", "ff", "dd")  
df2 = data.frame(n2, s2, b2)

 #   n2 s2 b2
 #1  5 aa hh
 #2  6 bb nn
 #3  7 cc ff
 #4  6 ll dd

I want to merge them to achieve the following result:

 #n s  b n2 s2 b2
 #2 aa 2 5  aa hh
 #3 bb 4 6  bb nn
 #5 cc 5 7  cc ff
 #5 dd 4 6  ll dd

Basically, what I want to achieve is to merge the two dataframes whenever the values in s of the first data is found in either the s2 or the b2 columns of data2.

I know that merge can work when I specify the two columns from each dataframe but I am not sure how to ADD the OR condition in the merge function. Or how to achieve this goal using other commands from packages such as dpylr.

Also, to clarify, there will be a situation where s2 and b2 have matches with s column in the same row. If this is the case, then just merge them once.

IRTFM

A coupld of problems: 1) you have built a couple of dataframes with factors which has a tendency to screw up matching and indexing, so I used stringsAsFactors =FALSE in hte dataframe calls. 2) you have an ambiguous situation with no stated resolution when both s2 and b2 have matches in the s column (as does occur in your example):

> df2[c("s")] <- list( c( df$s[pmax( match( df2$s2 , df$s), match(df2$b2, df$s),na.rm=TRUE)]))
> df2
  n2 s2 b2  s
1  5 aa hh aa
2  6 bb nn bb
3  7 cc ff ff
4  6 ll dd dd
> df2[c("s")] <- list( c( df$s[pmin( match( df2$s2 , df$s), match(df2$b2, df$s),na.rm=TRUE)]))
> df2
  n2 s2 b2  s
1  5 aa hh aa
2  6 bb nn bb
3  7 cc ff cc
4  6 ll dd dd

Once you resolve the ambiguity to your satiusfaction just use the same method to extract and match the "b"s:

> df2[c("b")] <- list( c( df$b[pmin( match( df2$s2 , df$s), match(df2$b2, df$s),na.rm=TRUE)]))
> df2
  n2 s2 b2  s b
1  5 aa hh aa 2
2  6 bb nn bb 4
3  7 cc ff cc 5
4  6 ll dd dd 4

Modified df's:

> dput(df)
structure(list(n = c(2, 3, 5, 5, 6, 7), s = c("aa", "bb", "cc", 
"dd", "ee", "ff"), b = c(2, 4, 5, 4, 3, 2)), .Names = c("n", 
"s", "b"), row.names = c(NA, -6L), class = "data.frame")
> dput(df2)
structure(list(n2 = c(5, 6, 7, 6), s2 = c("aa", "bb", "cc", "ll"
), b2 = c("hh", "nn", "ff", "dd"), s = c("aa", "bb", "cc", "dd"
), b = c(2, 4, 5, 4)), row.names = c(NA, -4L), .Names = c("n2", 
"s2", "b2", "s", "b"), class = "data.frame")

One step solution:

> df2[c("s", "c")] <-  df[pmin( match( df2$s2 , df$s), match(df2$b2, df$s),na.rm=TRUE), c("s", "b")]
> df2
  n2 s2 b2  s c
1  5 aa hh aa 2
2  6 bb nn bb 4
3  7 cc ff cc 5
4  6 ll dd dd 4

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Merge and concatenate two data frames in R

From Dev

R: Merge two data frames by common columns

From Dev

Merge two R data frames and identify the source of each row

From Dev

R issues with merge/rbind/concatenate two data frames

From Dev

Merge two data frames by a max number condition in r

From Dev

Matching two data frames in R

From Dev

Merge two data frames to fill in missing dates

From Dev

How to merge and sum two data frames

From Dev

Merge two data frames and select specific columns

From Dev

Merge two data.frames with replacement

From Dev

How to merge and compute two data frames?

From Dev

Merge two data frames on multiple values

From Dev

How to merge to two pandas data frames?

From Dev

Merge two data frames from a national survey with panel and not panel individuals of two different years (in r)

From Dev

Efficiently merging two data frames on a non-trivial criteria

From Dev

R merge function is unable to find shared matches between data frames

From Dev

how to rearrange an order of matches between two data frames

From Dev

Pandas: Creating data frames with non-repeating matches of two

From Dev

How to merge two data frames and pick lowest value from duplicated row in R

From Dev

Merge a single column in two data frames in R where only some rows match

From Dev

How to merge two data frames with different lengths by recycling without duplication in R?

From Dev

Pandas Data Frame - Merge Two Data Frames based on "InStr" > 0

From Dev

Extracting equal rows of two data frames (in R)

From Dev

Replace values between two data frames in R

From Dev

Multiplying and combine two data frames in R

From Dev

Substract two data.frames in R, by characters

From Dev

Dividing Two Data Frames (One into the Other) in R

From Dev

Divide a Column based on two Data Frames in R

From Dev

Multiplying and combine two data frames in R

Related Related

  1. 1

    Merge and concatenate two data frames in R

  2. 2

    R: Merge two data frames by common columns

  3. 3

    Merge two R data frames and identify the source of each row

  4. 4

    R issues with merge/rbind/concatenate two data frames

  5. 5

    Merge two data frames by a max number condition in r

  6. 6

    Matching two data frames in R

  7. 7

    Merge two data frames to fill in missing dates

  8. 8

    How to merge and sum two data frames

  9. 9

    Merge two data frames and select specific columns

  10. 10

    Merge two data.frames with replacement

  11. 11

    How to merge and compute two data frames?

  12. 12

    Merge two data frames on multiple values

  13. 13

    How to merge to two pandas data frames?

  14. 14

    Merge two data frames from a national survey with panel and not panel individuals of two different years (in r)

  15. 15

    Efficiently merging two data frames on a non-trivial criteria

  16. 16

    R merge function is unable to find shared matches between data frames

  17. 17

    how to rearrange an order of matches between two data frames

  18. 18

    Pandas: Creating data frames with non-repeating matches of two

  19. 19

    How to merge two data frames and pick lowest value from duplicated row in R

  20. 20

    Merge a single column in two data frames in R where only some rows match

  21. 21

    How to merge two data frames with different lengths by recycling without duplication in R?

  22. 22

    Pandas Data Frame - Merge Two Data Frames based on "InStr" > 0

  23. 23

    Extracting equal rows of two data frames (in R)

  24. 24

    Replace values between two data frames in R

  25. 25

    Multiplying and combine two data frames in R

  26. 26

    Substract two data.frames in R, by characters

  27. 27

    Dividing Two Data Frames (One into the Other) in R

  28. 28

    Divide a Column based on two Data Frames in R

  29. 29

    Multiplying and combine two data frames in R

HotTag

Archive