sample data
# Set seed for reproducibility
set.seed(123)
# Create a sample dataframe with 100 observations
n_obs <- 100
df <- data.frame(
serial_id = 1:n_obs,
code_1 = sample(c("yes", "no"), n_obs, replace = TRUE),
code_2 = sample(c("yes", "no"), n_obs, replace = TRUE),
code_3 = sample(c("yes", "no"), n_obs, replace = TRUE),
type_1 = sample(c("A", "B", "C", "D"), n_obs, replace = TRUE),
type_2 = sample(c("A", "B", "C", "D"), n_obs, replace = TRUE),
type_3 = sample(c("A", "B", "C", "D"), n_obs, replace = TRUE)
)
I am trying to create a variable that satisfies the following logic:
I could not figure out, how get the names of the corresponding column based on the last character (1,2,3,4,5,6.......), and then performing a rowwise operation taking into columns of that isolated row. The original data has such about 20 of such pair of code_* and type_*. So, I am trying to come up with something iterable.
Convert to long, cast to a wider format with the code and type paired, apply the rules by serial_id
with any()
, and then left join the new variable back to the original data set on serial_id
. This should work for as many code/type pairs as you have.
# Set seed for reproducibility
set.seed(123)
# Create a sample dataframe with 100 observations
library(data.table)
n_obs <- 100
df <- data.table(
serial_id = 1:n_obs,
code_1 = sample(c("yes", "no"), n_obs, replace = TRUE),
code_2 = sample(c("yes", "no"), n_obs, replace = TRUE),
code_3 = sample(c("yes", "no"), n_obs, replace = TRUE),
type_1 = sample(c("A", "B", "C", "D"), n_obs, replace = TRUE),
type_2 = sample(c("A", "B", "C", "D"), n_obs, replace = TRUE),
type_3 = sample(c("A", "B", "C", "D"), n_obs, replace = TRUE)
)
# Convert to long format
df_long <- melt(df, id.vars = 'serial_id')
df_long[, type := tstrsplit(variable, "_", keep = 1)]
df_long[, index := tstrsplit(variable, "_", keep = 2)]
# Cast to a paired wide format.
df_wide <- dcast(df_long, serial_id + index ~ type,
value.var = "value")
# Apply rules
# Rule 1) Any yes + A => new_var = 1.
df_wide[, new_var := ifelse(any(code == "yes" & type == "A"), 1, NA_real_),
by = serial_id]
# Rule 2) Any yes + B => new_var = 0, Overwrite rule 1, not rule 2
df_wide[, new_var := ifelse(any(code == "yes" & type == "B"), 0, new_var),
by = serial_id]
# Rule 3) all(no + B, no + C, yes + C) => new_var = 1. Overwrite rule 2.
# Example, serial_id = 38.
df_wide[, new_var :=
ifelse(any(code == "no" & type == "B") &
any(code == "no" & type == "C") &
any(code == "yes" & type == "C"), 1, new_var),
by = serial_id]
# Join back to the original data frame
df[df_wide[, .SD[1], by = serial_id],
new_var := new_var, on = .(serial_id)]