R/S-Plus: How can I merge two dataframes of different lengths?
November 19, 2010 2:06 PM Subscribe
Merging datasets in R. I seem to have forgotten how to do some things.
I have a dataframe of individual-level data in which rows are individuals are columns are variables (257,597 individuals in 87 countries) . I also have a dataframe of country-level data in which rows are countries and columns are variables. I want to merge these two dataframes so that individuals have the country-level data as well (meaning that the country-level value is repeated for each individual in a country). The problem -- the country-level dataframe has 182 countries, meaning that some of the countries don't match what's in the individual-level dataframe.
How can I merge these two dataframes without opening the country-level dataframe in Excel and removing by hand all of the countries that are not represented at the individual-level?
posted by proj to computers & internet (3 answers total) 2 users marked this as a favorite
You want to use the merge command. Next time post the top part of each dataset by doing head(dataset_name). Regardless, I will assume the individual_id exists in both dataframes
new_dataset=merge(individual_dataset,country_dataset,by="individual_id")
posted by special-k at 2:13 PM on November 19, 2010