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?
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?
Response by poster: You'd think I would have gotten that given the subject of this question contains the word merge. I feel really stupid now. Thanks.
posted by proj at 2:15 PM on November 19, 2010
posted by proj at 2:15 PM on November 19, 2010
Type in ?merge at the prompt for additional help.
posted by special-k at 2:15 PM on November 19, 2010
posted by special-k at 2:15 PM on November 19, 2010
This thread is closed to new comments.
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