-1

I have two tibbles (dplyr 0.7.4 and R 3.4.1) I need to do the following

for every variable b in A, I need to check if b is equal to any of the variables c in tibble C and if yes, assign variable d, if not assign NA.

  1. First match is fine, if there are multiple matches, it is ok to use the first
  2. The two tibbles A and C have different number of rows
  3. I would like to use a method that allows to do a custom comparison with a generic function fun().
  4. I would prefer to do this in dplyr but any method will do.

Tibble A:

b
var1_b
var2_b
...

Tibble C

c,d
var1_c, var1_d
var2_c, var2_d
...
mickkk
  • 1,172
  • 2
  • 17
  • 38
  • Please include the desired output for your input data in the question – talat Feb 07 '18 at 20:05
  • 1
    When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Feb 07 '18 at 20:15

1 Answers1

1

This should work

set.seed(1)
A <- data.frame(b = sample(1:40, 20, replace=TRUE))
C <- data.frame(c = sample(1:20, 40, replace=TRUE), d = 1:40)

myfun <- function(A, B) {
    sapply(A$b, function(i) B$d[match(i, B$c)])
}

A %>%
  mutate(new = myfun(A,C))

    # b new
# 1  11  25
# 2  15  19
# 3  23  NA
# 4  37  NA
# 5   9  20
CPak
  • 13,260
  • 3
  • 30
  • 48