merge_lr_var: Merge a variable that has distinct left and right...

Description Usage Arguments Value Examples

View source: R/data_io.R

Description

The input is a data frame with a variable that has left and right aspects. For example, the input data frame might contain the left and right variables man_I1_L and man_I1_R; the output data frame would contain a single, merged variable man_I1. The approach for merging variables must be input. There are five allowed approaches: "left", "right", "mean", "lowest", and "highest". "mean" should not be used for ordinal variables. For "left", the value of the left variable is used; if the left variable is NA for an observation and the right variable is not NA, the right variable is used. For "right", identical choices are made, except that right is preferred to left when available. For mean, the mean of the two values is used. For "lowest", the minimum value is used. For "highest", the maximum value is used.

Together, the three variables base_var, side_loc, and side_labels specify how the base variable (e.g., "man_I1") and left / right variables (e.g., "man_I1_L" / "man_I1_R") map onto each other. side_labels is a vector of length two where the first element gives the label used to distingush the left variable and the second elements gives the label used to distingush the right variable. side_loc must be either "start" or "end". If side_loc is "start", the left / right variables are created from the base variable by prefixing the two values in side_labels; if side_loc is "end", the left / right variables are created from the base variable by affixing the two values in side_labels. For example, if [base_ar = "man_I1], [side_labels = c("_L","_R")], and [side_loc = "end"], the left variable is [man_I1_L] and the right variable is [man_I1_R]. Conversely, if [side_labels = c("L_","R_")] and [side_loc = "start"], the left variable is [L_man_I1] and the right variable is [R_man_I1].

Usage

1
merge_lr_var(input_df, base_var, side_loc, side_labels, approach)

Arguments

input_df

Original data frame to be manipulated.

base_var

The base variable name to be merged

side_loc

Character string identifying if side labels are at the "start" or "end" of the base variable name

side_labels

Character string of left and right labels (in that order; ex.: c('_L','_R')).

approach

The merging approach to take (must be one of: "left", "right", "mean", "lowest", and "highest")

Value

The merged data frame

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
# Call merge_lr_var to combine left and right variable columns per
# user-specified approach
ex_df <- data.frame(ord_var_L = c(0,NA,1,NA,2,NA),
                    ord_var_R = c(2,1,0,NA,2,1),
                    L_cont_var = c(10.5,NA,14.0,NA,8.7,NA),
                    R_cont_var = c(12.5,3.2,7.0,NA,8.7,1.0))
first_merge <- merge_lr_var(input_df = ex_df,
                            base_var = "ord_var",
                            side_loc = "end",
                            side_labels = c("_L","_R"),
                            approach = "highest")
print(first_merge)

second_merge <- merge_lr_var(input_df = second_merge,
                             base_var = "cont_var",
                             side_loc = "start",
                             side_labels = c("L_","R_"),
                             approach = "mean")
print(second_merge)

MichaelHoltonPrice/yada documentation built on Sept. 19, 2021, 11:27 p.m.