mult_assign: Successively assign values to variables in a dataframe at...

View source: R/write_functions.R

mult_assignR Documentation

Successively assign values to variables in a dataframe at specified ids

Description

Successively assign values to variables in a dataframe at specified ids

Usage

mult_assign(df, id, df_cts)

Arguments

df

The dataframe to be modified

id

The name of the id column (character vector)

df_cts

A dataframe including the following columns: "var", "val" & "ids", such as generated, e.g., by tab_cts(df, include_ids = TRUE)

Value

The variables var in the dataframe df are set to val for all the ids in ids. This done for every tripple of these variables contained in the rows of df_cts

Examples

df <- data.frame(fbnr = 1:10,
             sex = c(2, 1, 2, 1, 1, 2, 2, 1, 2, 1),
             age = c(24, 23, 23, 41, 23, 39, 30, 18, 31, 48))
# Produce data summary:
df_cts <- tab_cts(df, "fbnr", include_ids = TRUE)
# Take the id column of df and reassign variables with all the values given in df_cts:
df_copy <- mult_assign(df["fbnr"], "fbnr", df_cts)
# Now both should be equal
all.equal(df, df_copy)

urswilke/tablab documentation built on Oct. 17, 2022, 8:19 p.m.