lundi 23 août 2021

R - adding a variable from another dataset with different # rows

I'm currently working on R on a survey on schools and I would like to add a variable with the population of the city the school is in.

In the first data set I have all the survey respondants which includes a variable "city_name". I have managed to find online a list of the cities with their population which I have imported on R.

What I now would like to do is to add a variable in dataset_1 called city_pop which is equal to the city population when city_name is in both data sets. It might be relevant to know that the first dataset has around 1200 rows while the second one has around 36000 rows.

I've tried several things including the following:

data_set_1$Pop_city = ifelse(data_set_1$city_name == data_set_2$city_name, data_set_2$Pop_city, 0)

Any clues?

Thanks!!

Aucun commentaire:

Enregistrer un commentaire