How to merge datasets in Stata conditionally?

R/S-Plus: How can I merge two dataframes of different lengths?

  • 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?

  • Answer:

    This is simple. 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")

proj at Ask.Metafilter.Com Visit the source

Was this solution helpful to you?

Other answers

You'd think I would have gotten that given the subject of this question contains the word merge. I feel really stupid now. Thanks.

proj

Type in ?merge at the prompt for additional help.

special-k

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.