0

I have two data sets. One with a numeric value assigned to individual categorical variables (country name) and a second with survey responses including a person's nationality. How do I assign the numeric value to a new column in the survey dataset with matching nationality/country name?

Here is the head of data set 1 (my.data1):

    EN           HCI
1 South Korea  0.845
2 UK           0.781
3 USA          0.762

Here is the head of data set 2 (my.data2):

         Nationality OIS IR
1        South Korea   2  2
2        South Korea   3  3
3                USA   3  4
4                 UK   3  3

I would like to make it look like this:

         Nationality OIS IR   HCI
1        South Korea   2  2 0.845
2        South Korea   3  3 0.845
3                USA   3  4 0.762
4                 UK   3  3 0.781

I have tried this but unsuccessfully:

my.data2$HCI <- NA
for (i in i:nrow(my.data2)) {
  my.data2$HCI[i] <- my.data1$HCI[my.data1$EN == my.data2$Nationality[i]]
}
D_Provost
  • 26
  • 2
  • Does this answer your question? [How to join (merge) data frames (inner, outer, left, right)](https://stackoverflow.com/questions/1299871/how-to-join-merge-data-frames-inner-outer-left-right) – astrofunkswag Mar 19 '20 at 18:20

1 Answers1

0

We can use a left_join

library(dplyr)
left_join(my.data2, my.data1, by = c("Nationality" = "EN"))

Or with merge from base R

merge(my.data2, my.data1, by.x = c("Nationality", by.y = "EN", all.x = TRUE)
akrun
  • 874,273
  • 37
  • 540
  • 662