0

I am working with a large dataset from the WVS survey which has many observations over 60 countries. I would like to add a variable “GDP” in which I assign to each row the GDP of the country from which the observation is. Note, each country has many observations. The problem is, my GDP vector has 60 rows, my dataframe around 89000, hence, I must assign the values to multiple rows. In a reduced example, it looks something like this:

country code  GDP
12            5128.2508
31            7181.7297
32            13386.7277

These I want to assign to my observations, which look something like this:

country code  X    
12            3
12            2
12            34
12            1
31            3
31            3
31            6
32            8

Such that my data frame looks in the end:

country code  X   GDP
12            3   5128.2508
12            2   5128.2508
12            34  5128.2508
12            1   5128.2508
31            3   7181.7297
31            3   7181.7297
31            6   7181.7297
32            8   13386.7277

So far, I have tried this by applying ifelse functions and put in the values of the GDPs by hand. However, this cannot be the best solution. Plus, because I have so many countries, I have to split my ifelse loops, which then results in errors in the assignments when I bind them together. Do you know the right way to assign/combine these values? Thanks already!

This is part of my code for the loops:

GDP_part1 <- ifelse(cc==12, 5128.2508, ifelse(cc==32, 13386.7277, ifelse(cc==51, 3551.6805, ifelse(cc==36, 61264.396, ifelse(cc==31, 7181.7297, 
                                                                                                                             ifelse(cc==112, 6863.866, ifelse(cc==76, 11485.4807, ifelse(cc==152, 14326.3207, ifelse(cc== 156, 6523.4312, ifelse(cc==170, 7204.133,
                                                                                                                                                                                                                                                 ifelse(cc==196, 27523.2473, ifelse(cc==218, 5635.0947, ifelse(cc==818, 3080.0176, ifelse(cc==233, 17820.5285, ifelse(cc==268, 3868.8117,
                                                                                                                                                                                                                                                                                                                                                                      ifelse(cc==276, 44684.5912, ifelse(cc==288, 1568.7862, ifelse(cc==332, 779.1582, ifelse(cc==344, 37376.984, ifelse(cc==356, 1534.231,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         ifelse(cc==368, 5864.4707, ifelse(cc==392, 40527.1765, ifelse(cc==400, 4985.6448, ifelse(cc==398, 11542.9447, ifelse(cc==410, 25373.713,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              ifelse(cc==414, 39732.0767, ifelse(cc==417, 1147.132, ifelse(cc==422, 10259.6707, ifelse(cc==434, 9012.6592, ifelse(cc==458, 10290.8555,NA ))))))))))))))))))))))))))))))
```
      
Anna
  • 1
  • If the two dataframes are called `df1` and `df2`. Try `df3 <- merge(df1, df2)`. – Ronak Shah Mar 22 '21 at 10:27
  • Use a left join: `merge(DF2, DF1, all.x = TRUE, all.y = FALSE)` assuming (1) you want one row of output for every row of DF2 and (2) there may be countries listed in DF1 that are not in DF2 and we don't want output rows for them. – G. Grothendieck Mar 22 '21 at 10:39

0 Answers0