1

I have two tables that I want to merge:

Data.frame A:

DATUM CP MNOZSTVI CLOSE
2021-07-30 USD 18 NA
2021-07-30 CZK 2991 NA
2021-07-30 EUR 0 NA

Data.frame B:

DATUM CP CLOSE
2021-07-30 USD 21,4
2021-07-30 CZK 1
2021-07-30 EUR 25

Desired data.frame:

DATUM CP MNOZSTVI CLOSE
2021-07-30 USD 18 21,4
2021-07-30 CZK 2991 1
2021-07-30 EUR 0 25

I would like to do that using join function from dplyr. I tried it using full_join:

desired.data.frame<- A %>%
  full_join(B)

But it does not work as I want. Thank you for your help guys.

1 Answer 1

1

You could use left_join and then select the columns:

library(dplyr)
df_A %>% 
    left_join(df_B, by="CP") %>% 
    select(DATUM = DATUM.x, CP, MNOZSTVI, CLOSE=CLOSE.y)

Or thanks to akrun ,

df_A %>% select(-CLOSE) %>% left_join(df_B)

Output:

 DATUM      CP    MNOZSTVI CLOSE
  <chr>      <chr>    <dbl> <dbl>
1 2021-07-30 USD         18  21.4
2 2021-07-30 CZK       2991   1  
3 2021-07-30 EUR          0  25 

data:

df_A <- tribble(
~DATUM,     ~CP,    ~MNOZSTVI, ~CLOSE,
"2021-07-30",   "USD",  18, NA,
"2021-07-30",   "CZK",  2991, NA,
"2021-07-30",   "EUR",  0, NA)

df_B <- tribble(
    ~DATUM,     ~CP,    ~CLOSE,
    "2021-07-30",   "USD",  21.4,
    "2021-07-30",   "CZK",  1,
    "2021-07-30",   "EUR",  25)
df_A
Sign up to request clarification or add additional context in comments.

2 Comments

Or may do the select before df_A %>% select(-CLOSE) %>% left_join(df_B)
Perfect. Thanks master!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.