0

Suppose I have 2 different datasets for countries. Both have same countries, but slightly different:

dataset A:

col1   covid_cases  region   

russia    2          2

israel    3          1

russia    2          3

russia    2          4

russia    2          1

russia    2          6

dataset B:

col1   covid_cases  income   

russia    2          low

russia    2          low

israel    3          high

The region column and income column are independent.

In my original datasets I have 100 countries.

What's an efficient way to get this type of dataset:

col1   covid_cases  region   income

russia    2          2          low

israel    3          1          high

russia    2          3           low

russia    2          4           low

russia    2          1           low

russia    2          6           low

So order here in the dataset doesn't matter. I'm not interested in simply just taking one column from one dataset and adding it to another. I'm interested in adding the income column so that its values matches the countries income, just like in dataset 2.

user
  • 235
  • 2
  • 16

1 Answers1

1

Maybe try this:

library(dplyr)
#Code
newdf <- df1 %>% left_join(df2 %>% select(-c(covid_cases)) %>%
                    filter(!duplicated(col1)))

Output:

    col1 covid_cases region income
1 russia           2      2    low
2 israel           3      1   high
3 russia           2      3    low

Using your new dataframes, the code will work too:

    col1 covid_cases region income
1 russia           2      2    low
2 israel           3      1   high
3 russia           2      3    low
4 russia           2      4    low
5 russia           2      1    low
6 russia           2      6    low
Duck
  • 39,058
  • 13
  • 42
  • 84
  • my datasets don't have the same amount of rows – user Nov 20 '20 at 13:47
  • @Marina Could you please add a more elaborated example to understand what you want? – Duck Nov 20 '20 at 13:48
  • I added a different dataframe. Am I still alowed to use your method, even if the 2 dataframes don't have the same length? – user Nov 20 '20 at 13:51
  • @Marina Hi dear, yes it will work. I have tested with your new data and the output is as the one you want! Let me know any other doubt :) – Duck Nov 20 '20 at 14:00