Home > Blockchain >  How can i merge these 2 dataframes into one whilst matching data to rows?
How can i merge these 2 dataframes into one whilst matching data to rows?

Time:02-16

I have 2 dataframes which i need to make into one. The first is 'GDP' and the second is 'Death_rates'. I want to combine the data from both matching up to the country and year columns. Assume i need to use rbind but not sure on how to match up the data from the 2nd dataset with the first.

GDP has colnames of 'Country' 'Year' 'GDP' Death_rates has colnames of 'Country' 'Year' 'Total deaths' 'Indoor deaths' 'Particlate matter' 'Ozone'

Intending to get one dataframe with 'Country' 'Year' 'Total deaths' 'Indoor deaths' 'Particlate matter' 'Ozone' 'GDP'

CodePudding user response:

If the column names are Country and Year in both data frames, you can use

merge(data1, data2, by=c("Country", "Year"))

If they are different, use by.x and by.y.

  • Related