recode_sex
recodes a binary sex variable for a given source column in a data frame or data table.
Useful in transforming output from growthcleanr::cleangrowth() into a format suitable for growthcleanr::ext_bmiz().
recode_sex(
input_data,
sourcecol = "sex",
sourcem = "0",
sourcef = "1",
targetcol = "sex_recoded",
targetm = 1L,
targetf = 2L
)
a data frame or data table to be transformed. Expects a source column containing a binary sex variable.
name of sex descriptor column. Defaults to "sex"
variable indicating "male" sex in input data. Defaults to "0"
variable indicating "female" sex in input data. Defaults to "1"
desired name of recoded sex descriptor column. Defaults to "sex_recoded"
desired name of recoded sex variable indicating "male" sex in output data. Defaults to 1
desired name of recoded sex variable indicating "female" sex in output data. Defaults to 2
Returns a data table with recoded sex variables.
# Run on given data
df <- as.data.frame(syngrowth)
# Run with all defaults
df_r <- recode_sex(df)
# Specify different targets
df_rt <- recode_sex(df, targetcol = "sexr", targetm = "Male", targetf = "Female")
# Specify different inputs
df_ri <- recode_sex(df_rt, sourcecol = "sexr", sourcem = "Male", sourcef = "Female")