var_wide_df <- data.frame(
    Var1_E1_C1 = c(1, NA, NA, 1, 1),
    Var1_E1_C2 = c(NA, 3, 2, NA, NA),
    Var23_E2_C1 = c(NA,2 , NA, 1, 1),
    Var23_E2_C2 = c(3,NA , 1, NA, NA)
)

var_long_df <- reshape(
    var_wide_df,
    varying = colnames(var_wide_df),
    times = colnames(var_wide_df),
    v.names = "value",
    timevar = "variable",
    direction = "long",
    new.row.names = 1:1E7
) |> within({
    x <- strsplit(variable, "_")

    Var <- sapply(x, `[`, 1)
    E <- sapply(x, `[`, 2)
    C <- sapply(x, `[`, 3)
    VarE <- paste(Var, E, sep="_")
    rm(x)
}) |> (
    \(df) aggregate(
        formula = value ~ id + VarE,
        data = df,
        FUN = max, na.rm = TRUE,
        na.action = na.omit
    )
)()

var_long_df

new_var_df <- reshape(
    var_long_df,
    idvar = "id",
    v.names = "value",
    timevar = "VarE",
    direction = "wide",
    sep = "_"
)

new_var_df

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: