As you build a dataset you may want to define its columns in a meta-dataset. After some time, the dataset and its meta-dataset may fall out of sync, with the row order of the meta-dataset no longer matching the column order of the dataset. Here is how to fix it.
library(tibble)
dataset <- tibble(
col1 = 1:3,
col2 = letters[1:3],
col3 = LETTERS[1:3]
)
dataset
## # A tibble: 3 x 3
## col1 col2 col3
## <int> <chr> <chr>
## 1 1 a A
## 2 2 b B
## 3 3 c C
mixed_rows <- tibble::tribble(
~cols, ~definition,
"col3", "Some LETTERS",
"col1", "Some numbers",
"col2", "Some letters"
)
mixed_rows
## # A tibble: 3 x 2
## cols definition
## <chr> <chr>
## 1 col3 Some LETTERS
## 2 col1 Some numbers
## 3 col2 Some letters
Fix.
ordered_cols <- match(names(dataset), mixed_rows$cols)
ordered_rows <- mixed_rows[ordered_cols, ]
ordered_rows
## # A tibble: 3 x 2
## cols definition
## <chr> <chr>
## 1 col1 Some numbers
## 2 col2 Some letters
## 3 col3 Some LETTERS