Is there a way in R to transfer values from multiple data frames to a second data frame if certain conditions are met? Specifically, I am concerned with the following problem:
I have values for different people in data frame 1 (columns: name, country in ISO-3, year1, year2), polity scores for all countries in data frame 2 (columns: country in ISO-3, year, score) and gdp for all countries in data frame 3 (columns: country in ISO-3, year, gdp)
I would now like to add the corresponding polity scores from data frame 2 and gdp value from data frame 3 to data frame 1 as a new columns depending on country and year for year1 and year 2. Can I automate this via R?
df1
name | country | year1 | year2 | polity_year1 | gdp_year1 | polity_year2 | gdp_year2 |
---|---|---|---|---|---|---|---|
Name 1 | USA | 1990 | 2018 | polity score: year1 | gdp: year1 | polity score: year2 | gdp: year2 |
Name 2 | DNK | 1995 | 1999 | polity score: year1 | gdp: year1 | polity score: year2 | gdp: year2 |
df2
country | year | score |
---|---|---|
USA | 1990 | 10 |
USA | 2018 | 10 |
DNK | 1995 | 10 |
DNK | 1999 | 10 |
df3
country | year | gdp |
---|---|---|
USA | 1990 | 5325 |
USA | 2018 | 7230 |
DNK | 1995 | 2245 |
DNK | 1999 | 3256 |
I tried dplyr join(),
df_joined <- left_join(df1, df2, by = c("country" = "country", "year_1" = "year"), name="polity_year1") %>%
left_join(df1, df2, by = c("country" = "country", "year_2" = "year"),name="polity_year2") %>%
left_join(df1, df3, by = c("country" = "country", "year_1" = "year"), name="gdp_year1") %>%
left_join(df1, df3, by = c("country" = "country", "year_2" = "year"), name="gdp_year2")
but that fails for several reasons
- I can only run the function for one year of df1
- I can't run the function using %>% for df2 (polity) and df3 (gdp) at the same time
- I can't set the new column names in df1 depending on year in df1 (e.g. "polity_year1" or "gdp_year2")
Any help would be greatly appreciated.
%>%
thed1
inleft_join(df1, df2, ...
should be removed, otherwise R will see three values for a join, when only two are allowed. – Tech Commodities