I have two datasets. They refer to the same data. However, one has string as answers to questions, and the other has the corresponding codes.
library(data.table)
dat_string <- fread("str_col1 str_col2 numerical_col
One Alot 1
Two Alittle 0")
dat_codes <- fread("code_col1 code_col2 numerical_col
0 3 1
1 5 0")
I would like, to combine both datasets, so that the levels get attached to the corresponding codes as labels
, (see this example) for all string columns (in dat_string
).
Please note that the column names can have any format and do not necessarily have the format from the example/
What would be the easiest way to do this?
Desired outcome:
dat_codes$code_col1 <- factor(dat_codes$code_col1, levels=c("0", "1"),
labels=c("One", "Two"))
attributes(dat_codes$code_col1)$levels
[1] "One" "Two"