Add variables from one data.frame to another
Source:R/dataprep.R
add_vars_from_one_df_to_another.RdThis function adds some variables from one data.frame to another, by matching rows by a given variable.
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 fromby_to)- vars
(character) vector of variable names to add to
df_to
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).
Examples
if (FALSE) {
df <- df |> add_vars_from_one_df_to_another(ref_table, by_to="id", vars=c("Gender", "Age"))
}