0

Suppose I have a specific value x (unique) that is contained in df1. I want to match this value x (unique) with the value x (unique) contained in df2 so to obtain the correspondent value "date". The problem is that df1 and df2 have two different row lengths, thus the following function won't work:

match(df1$x,df2$x)

df2$date[match(df1$x,df2$x)]

df1$date = df2$date[match(df1$x,df2$x)]

Example df1 and df2:

df1-->


    x | y | a | b |
    1   2   6   9
    2   2   7   2
    3   4   8   1 
    4   5   7   2

df2-->


   x | z | date | l | m |
   1   2   1987   a   c
   2   2   1989   b   c 
   3   2   1986   a   d

Example final result: df1 -->


    x | y | date 
    1   2   1987 
    2   2   1989
    3   2   1986 
Boletus
  • 81
  • 6
  • A way to solve this is given by this function (credits to a co-worker): df1 <- merge(df1, df2, by="x") However, I get also a bunch of unrelated data that I did not really ask for(since the data frames have more columns than that). – Boletus Apr 25 '19 at 23:08
  • check edited answer, you need ```dplyr::inner_join``` – c1au61o_HH Apr 25 '19 at 23:58

1 Answers1

0

Would something like df1$date[df1$date %in% df2$date] work?

EDITED after comment

I see. Now I got it.

library(dplyr)

df1 <- data.frame(x = 1:4, y = rep(c(2,4), each=2))
df2 <- data.frame(x = 1:3, z = c(2,2,2), date = 1986:1988)

inner_join(df1, df2, by = c("x")) %>% 
  select(-z)
#>   x y date
#> 1 1 2 1986
#> 2 2 2 1987
#> 3 3 4 1988

Created on 2019-04-25 by the reprex package (v0.2.1)

c1au61o_HH
  • 867
  • 7
  • 14