Is there a way in R to transfer values from one data frame 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, year of birth, place of birth in ISO-3) and the polity scores for different countries in data frame 2 (columns: country in ISO-3, year, score). I would now like to add the corresponding polity scores from data frame 2 to data frame 1 as a new column depending on country and year. Can I automate this via R?
Data frame 1
Name | Country of Birth | Year of Birth | Polity Score |
---|---|---|---|
Name 1 | USA | 2018 | Score from data frame 2 |
Name 2 | DNK | 1995 | Score from data frame 2 |
Data frame 2
Country | Year | Polity Score |
---|---|---|
USA | 2018 | 10 |
DNK | 1995 | 10 |
You need to join the 2 tables up, there are lots of methods and packages to do this but I am always a fan of the tidyverse, in this case dplyr
joins.
Without seeing your table specifics it will look something like this.
df_joined <- left_join(df1, df2, by = c("Country of Birth" = "Country", "Year of Birth" = "Year")
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments