0

My R knowledge is very limited. For a paper I'm working on, I need to combine two data sets (openness of trade and co2 output) to do a regression analysis.

These are the data sets https://ourworldindata.org/grapher/trade-openness https://github.com/owid/co2-data, which I converted to .xlsx (https://1drv.ms/x/s!ArVyXA5cSMj2h6pUWPU9ns2UkJW-ww?e=8jne7b & https://1drv.ms/x/s!ArVyXA5cSMj2h6pSE9lx6DNxhVOang?e=TqQwbI).

I imported both data sets to R (had to use the "import datasheet" tool in RStudios, because I was getting this error when trying to import them via comand), renamed them and made sure all matching columns were named the same:

open <- trade_openness co2 <- owid_co2_data_1 rm(trade_openness) rm(owid_co2_data_1)

names(open)[1] <- "country"  
names(open)[2] <- "iso_code"  
names(open)[3] <- "year"   
names(open)[4] <- "openness"   

Now I wanted to merge both datasets to have the trade openness data next to the co2 data. For example

# | country | iso_code | year | openness | co2 | co2_growth_prct | ...

After watching some youtube guides on merging data frames I tried this function:

merge(open, co2, by = "country", "year", all.x = TRUE)

This did something, but not what I was hoping for. Also I noticed it left the existing data frames untouched, so I'm guessing I need to add a comand to create a new, merged data frame?

I feel like this should be relatively simply, but I don't know how do this. Could someone please help me out with this?

Best regards!

  • Yes you need to assign the output of the merge to a new object, this might work but as I can't see your data I'm not 100%. `merged_data <- merge(open, co2, by = c("country", "year"), all.x = T)` – Tob Jul 27 '21 at 09:58
  • See : [How to join (merge) data frames (inner, outer, left, right)](https://stackoverflow.com/questions/1299871/how-to-join-merge-data-frames-inner-outer-left-right) – Ronak Shah Jul 27 '21 at 10:07

2 Answers2

0

Try this : merged_data <- merge(open, co2, by = c("country", "year) or merged_data <-rbind (open, co2).

  • Thanks, this worked! New data frame is pretty messy, but all the data I need is there, so I should be able to sort/rename everything properly to work with it! – Jakob Meyer Jul 27 '21 at 10:46
0

your new data set is here called merge. Rememer to change the file path i.e "C:/Users/Downloads/"

trade <- readxl::read_excel("C:/Users/Downloads/trade-openness.xlsx")

owid <- readxl::read_excel("C:/Users/Downloads/owid-co2-data-1.xlsx")

library(tidyverse)

merge <- left_join(trade, owid)

merge
Dr. Flow
  • 456
  • 5
  • 19