I have this table that contains variables that are one hot encoded. I want to collapse these variables into one column. For example, any columns that have "high", "med", or "low", I want to be one column with numerical encodings for high = 0, med = 1, and low = 2. How can I do this in dplyr R? I suspect pivoting will help but I'm not sure where to start. The resulting column name should contain the name of the three columns without the high,med,low designation.
For example, I would transform columns d-high_cm1, d-med_cm1, d-low_cm1 to d-cm1 with the numerical encodings.
input:
sex age cost_cm d-high_cm1 d-med_cm1 d-low_cm1 c-high_cm1 c-med_cm1 c-low_cm1
f old 1 1 0 0 1 0 0
m young 0 1 0 0 1 0 0
m old 0 0 1 0 0 1 0
f young 0 1 0 0 0 0 1
m old 1 0 0 1 0 0 1
expected output:
sex age cost_cm d-cm1 c-cm1
f old 1 0 0
m young 0 0 0
m old 0 1 1
f young 0 0 2
m old 1 2 2