0

I recently work with data sets that are in the following order:

country  year ...
Albania  2012 ...
Albania  2013 ...
Albania  2014 ...
Albania  2015 ...
Albania  2016 ...
Bulgaria 2012 ...
Bulgaria 2013 ...
Bulgaria 2014 ...
Bulgaria 2015 ...
Bulgaria 2016 ...
...      ...
...      ...

I often need to add a new column to the data set before analysis, but the presence of the year column make it slightly more complicated for me. Imagine a second data set like:

country  year   gdp
Albania  1970 12111
Albania  1971 12222
...      ...    ...
Albania  2016 22333
...      ...    ...
Bulgaria 1970 15333
Bulgaria 1971 15444
...      ...    ...

How can I merge the above two data sets in a way that makes sure that the gdp value goes to right country-year row?

Thank you.

neutral
  • 107
  • 4
  • 13

1 Answers1

1

Let your first dataset be df1 and the second be df2

df3 <- merge(x=df1,y=df2,by=c("country","year"),all.x=TRUE)

Which is a left outer join (Assuming you have gdps for all years present in df1)

See here for more info: How to join (merge) data frames (inner, outer, left, right)?

Community
  • 1
  • 1
Buzz Lightyear
  • 824
  • 1
  • 7
  • 18