I have collected a dataset with coded values and a decoding table with the actual values, and now I need to decode the dataset.
Here's an example to illustrate my challenge:
Coded Dataset
## # A tibble: 3 x 4
## FRUIT NUM NAME ORDINAL
## <dbl> <dbl> <dbl> <dbl>
## 1 1. 1. 1. 1.
## 2 2. 2. 2. 2.
## 3 3. 3. 3. 4.
Decoding Table
## # A tibble: 13 x 3
## VAR CODE VALUE
## <chr> <dbl> <chr>
## 1 FRUIT 1. apple
## 2 FRUIT 2. banana
## 3 FRUIT 3. cherry
## 4 NUM 1. one
## 5 NUM 2. two
## 6 NUM 3. three
## 7 NAME 1. Adam
## 8 NAME 2. Barb
## 9 NAME 3. Chad
## 10 ORDINAL 1. first
## 11 ORDINAL 2. second
## 12 ORDINAL 3. third
## 13 ORDINAL 4. fourth
How can I transform my dataset to return the following result?
Desired Result
## # A tibble: 3 x 4
## FRUIT NUM NAME ORDINAL
## <chr> <chr> <chr> <chr>
## 1 apple one Adam first
## 2 banana two Barb second
## 3 cherry three Chad fourth
The actual dataset has approximate 50 columns, so I'm looking for a solution that avoids recoding each column one at a time.
Thanks!
Reprex
``` r library(tidyverse)(tbl <-
tribble(
~ FRUIT, ~ NUM, ~ NAME, ~ORDINAL,
1, 1, 1, 1,
2, 2, 2, 2,
3, 3, 3, 4
))
# A tibble: 3 x 4
FRUIT NUM NAME ORDINAL
1 1. 1. 1. 1.
2 2. 2. 2. 2.
3 3. 3. 3. 4.
(lookup <-
tribble(
~ VAR, ~ CODE, ~ VALUE,
"FRUIT", 1, "apple",
"FRUIT", 2, "banana",
"FRUIT", 3, "cherry",
"NUM", 1, "one",
"NUM", 2, "two",
"NUM", 3, "three",
"NAME", 1, "Adam",
"NAME", 2, "Barb",
"NAME", 3, "Chad",
"ORDINAL", 1, "first",
"ORDINAL", 2, "second",
"ORDINAL", 3, "third",
"ORDINAL", 4, "fourth"
))
# A tibble: 13 x 3
VAR CODE VALUE
1 FRUIT 1. apple
2 FRUIT 2. banana
3 FRUIT 3. cherry
4 NUM 1. one
5 NUM 2. two
6 NUM 3. three
7 NAME 1. Adam
8 NAME 2. Barb
9 NAME 3. Chad
10 ORDINAL 1. first
11 ORDINAL 2. second
12 ORDINAL 3. third
13 ORDINAL 4. fourth
(result <-
tribble(
~ FRUIT, ~ NUM, ~ NAME, ~ORDINAL,
"apple", "one", "Adam", "first",
"banana", "two", "Barb", "second",
"cherry", "three", "Chad", "fourth"
))
# A tibble: 3 x 4
FRUIT NUM NAME ORDINAL
1 apple one Adam first
2 banana two Barb second
3 cherry three Chad fourth
</details>