I'm trying to replace some dummy coded variables with the actual categorical value. I know there's ways to replace this using base R, dplyr, and the *apply or within functions, but my brain just can't figure out which is most efficient without looping it.
Currently, I have 2 data frames. Examples of what they look like below.
Note: Instead of 5 variables, there are closer to 10+ and this operation needs to be done multiple times so looping or a simple df <- df[df$var == 1] <- "a" won't work.
df1
# id v1 v2 v3 v4 v5
# 1 1 0 1 0 1 0
# 2 3 0 0 0 0 1
# 3 5 0 1 1 0 0
# 4 12 1 0 1 0 0
# 5 26 0 0 0 1 0
...
codes
# name label
# 1 v1 "a"
# 2 v2 "b"
# 3 v3 "c"
# 4 v4 "d"
# 5 v5 "e"
The goal is to get the values in codes$label to replace the 1's according to the relevant column name in df1 so the end result is:
df1
# id v1 v2 v3 v4 v5
# 1 1 0 b 0 d 0
# 2 3 0 0 0 0 e
# 3 5 0 b c 0 0
# 4 12 a 0 c 0 0
# 5 26 0 0 0 d 0
I know there's a way to do it, but I can't find any code snippets online that do it over multiple columns and when referencing another dataframe.
Thank you!
P.S. These are both technically tibbles() formatted with tidyverse