R: How can I transfer values from one data frame to another data frame depending on certain circumstances?

Max

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
Quixotic22

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.

edited at
0

Comments

0 comments
Login to comment

Related

Creating variable in R data frame depending on another data frame

In R, how do you classify values in one data frame based on ranges in another data frame?

How to define calculations in a data frame depending on values from another data frame?

Add value from one data frame into another data frame in R

Using grep to substitute values from one data frame to another in R

How to use values from one data frame to inform a summing function within another data frame in R?

How to subtract one record from another data frame in R

Check if value from one data frame can be found in another data frame in R

Copy data from one data-frame to another and substitute the data depending on values

In R is there a way to recode the columns from one data frame with values from another data frame?

In R, how do I match values from selected rows in one data frame with selected columns in another?

in R, how can I keep all rows from one data frame based on groups from a second data frame?

How to move data from one frame to another?

How to match values from a data frame to another

In R, how do I select all variables from one data frame that are in another data frame?

Is there a way in R to UPDATE one data frame from another data frame?

Make a new column picking values from a data frame depending on the values in the same data frame in R

How can I see if any values in one data frame exist in any other data frame?

R: How can I transfer values from multiple data frames to another data frame depending on certain conditions?

R - use apply to transfer one value from one data frame to another by match of two columns

Count how many times strings from one data frame appear to another data frame in R dplyr

how can I take certain elements in a larger data frame and make another data Frame with these elements in python?

How can i match the values of a column according to another of a data frame in R using dplyr?

How I can calculate the variance across all columns in a data frame in R according to the values of another data frame using Dplyr?

How can I recode several values based on another data frame in several variables in R?

In R, how can I add values onto the end of rows where one value matches that of another data frame?

How do I assign values from one data frame to another based on row value using R?

How can I transpose a data frame in R so that a certain column becomes column names and another column fills the values?

How do I transfer NA's from one data frame into another data frame of the same size in R