I have a data set with many columns (DATA_OLD) in which I want to exchange all values based on an allocation list with many entries (KEY).
Every value in DATA_OLD should be replaced by its counterpart (can be seen in KEY) to create DATA_NEW.
For simplicity, the example here contains a short KEY and DATA_OLD set. In reality, there are >2500 rows in KEY and >100 columns in DATA_OLD. Therefore, an approach that can be applied to the whole data set simultaneously without calling each colname of DATA_OLD is important.
KEY:
old | new |
---|---|
1 | 1 |
3 | 2 |
7 | 3 |
12 | 4 |
55 | 5 |
Following this example, every value "1" should be replaced with another value "1". Every value "3" should be replaced with value "2". Every value "7" should be replaced with value "3".
DATA_OLD (START):
var1 | var2 | var3 |
---|---|---|
NA | 3 | NA |
NA | 55 | NA |
1 | NA | NA |
NA | NA | NA |
3 | NA | NA |
55 | NA | 12 |
DATA_NEW (RESULT):
var1 | var2 | var3 |
---|---|---|
NA | 2 | NA |
NA | 5 | NA |
1 | NA | NA |
NA | NA | NA |
2 | NA | NA |
5 | NA | 4 |
Here reproducible data:
KEY<-structure(list(old = c(1, 3, 7, 12, 55), new = c(1, 2, 3, 4,
5)), class = "data.frame", row.names = c(NA, -5L))
DATA_OLD<-structure(list(var1 = c(NA, NA, 1, NA, 3, 55), var2 = c(3,
55, NA, NA, NA, NA), var3 = c(1, NA, NA, NA, NA, 12)), class = "data.frame", row.names = c(NA, -6L))
DATA_NEW<-structure(list(var1 = c(NA, NA, 1, NA, 2, 5), var2 = c(2,
5, NA, NA, NA, NA), var3 = c(1, NA, NA, NA, NA, 4)), class = "data.frame", row.names = c(NA, -6L))
I have tried back and forth, and it appears that I am completely clueless. Help would be greatly apprecciated! The real data set is quite large...