Citizen
Citizen

Reputation: 121

merge tibbles by column with repeated values

This is a particular "merge tibbles" case. My reproducible example is as follows. I have the following tibbles:

> tb1
# A tibble: 4 x 2
# Groups:   symbol [2]
  symbol tarima   
  <chr>   <dbl>   
 1 A        59.5
 2 A        61.7
 3 AA       33.1
 4 AA       32.5
 5 AAL      49.6  
 6 AAL      51.6  

> tb2
# A tibble: 6 x 4
# Groups:   symbol [3]
  symbol  open  high   low
  <chr>  <dbl> <dbl> <dbl>
1 A       60.8  61.2  60.8
2 A       60.3  60.5  59.1
3 AA      32.4  33    32.3
4 AA      33.6  34    32.8

I would like to get the output:

# A tibble: 4 x 5
# Groups:   symbol [2]
  symbol      open  high   low    tarima
  <chr>       <dbl> <dbl> <dbl>    <dbl>
 1 A           60.8  61.2  60.8    59.5
 2 A           60.3  60.5  59.1    61.7
 3 AA          32.4  33    32.3    33.1
 4 AA          33.6  34    32.8    32.5

I have to keep the same order.

I would be grateful if someone could help me with this issue

Upvotes: 0

Views: 236

Answers (2)

Pascual Esteban
Pascual Esteban

Reputation: 23

Maybe I'm a bit late, but I hope it can be useful to anyone else

I think you can achieve what you want by using the join functions provided by dyplr.

Using right_join you would write:

new_tb <- right_join(tb1, tb2, by="symbol")

This should keep all rows of tb2 and all columns of tb1, merging the tibbles when the value of the variable "symbol" matches in both.

Upvotes: 1

tmfmnk
tmfmnk

Reputation: 39858

A tidyverse possibility:

bind_cols(df2, df1 %>%
 filter(symbol %in% df2$symbol)) %>%
 select(-symbol1)

  symbol open high  low tarima
1      A 60.8 61.2 60.8   59.5
2      A 60.3 60.5 59.1   61.7
3     AA 32.4 33.0 32.3   33.1
4     AA 33.6 34.0 32.8   32.5

It filters out the values from "symbol" in df1 that are not in "symbol" in df2 and binds the two dfs by columns.

Upvotes: 1

Related Questions