Joe
Joe

Reputation: 87

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

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'

Upvotes: 2

Views: 532

Answers (1)

cdalitz
cdalitz

Reputation: 1277

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.

Upvotes: 0

Related Questions