Skip to contents

This function adds some variables from one data.frame to another, by matching rows by a given variable.

Usage

add_vars_from_one_df_to_another(df_to, df_from, by_to, by_from = NULL, vars)

Arguments

df_to

data.frame in which to add the wanted variables

df_from

data.frame that contains the wanted variables

by_to

(character) matching variable in df_to

by_from

(character, optional) matching variable in df_from (if different from by_to)

vars

(character) vector of variable names to add to df_to

Value

df_to with columns added (if any row match by by)

Details

This is a convenience function using merge(), with only selected variables merged into the first data.frame, and with the order of columns preserved. The matching variable may or may not be named the same in both data.frames (as controlled by by_to and by_from).

See also

Examples

if (FALSE) {
df <- df |> add_vars_from_one_df_to_another(ref_table, by_to="id", vars=c("Gender", "Age"))
}