-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: write rename columns post and fix quarto chunks arguments
- Loading branch information
1 parent
50dc9cd
commit c143654
Showing
7 changed files
with
137 additions
and
57 deletions.
There are no files selected for viewing
4 changes: 2 additions & 2 deletions
4
_freeze/posts/2023-10-08_datetimes-openxlsx/index/execute-results/html.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
_freeze/posts/2023-10-08_rename-columns-lookup/index/execute-results/html.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"hash": "2c1b7cd21ed9922aade253ecf8ae1982", | ||
"result": { | ||
"markdown": "---\ntitle: \"Rename variables in a data frame using an external lookup table\"\nauthor: \"Layal C. Lettry\"\ndate: \"2023-10-08\"\ncategories: [unquote-splice, tidy evaluation, rename, any_of]\nimage: \"image.jpg\"\n---\n\n\n# Rename variables in a data frame using an external lookup table\n\nSuppose that a data frame is present with certain columns that possess the appropriate names, however, the remaining columns require renaming. An existing lookup table is ready to be used for setting new names to these specific columns. \n\nI found the solution by using tidy evaluation tools, namely the unquote-splice `!!!`, and by reading the [article written by Tim Tiefenbach](https://tim-tiefenbach.de/post/2022-rename-columns/#dplyr-tidyverse). \n\n\n::: {.cell}\n\n```{.r .cell-code}\nlibrary(tidyverse)\n```\n:::\n\n\nHere is the data frame with 3 variables, namely `var1`, `var2` and `var4`.\n\n\n::: {.cell}\n\n```{.r .cell-code}\ntest_tib <- tribble(\n ~var1, ~var2, ~var4,\n \"x\", \"a\", 1L,\n \"y\", \"b\", 2L,\n \"z\", \"c\", 3L\n)\n```\n:::\n\n\nDefine the lookup table with the new names. Transform this lookup table into a named vector using `deframe()`. Do not forget that the first argument of `deframe()` should be the new names of the variable and the second one should have the actual names.\n\n\n::: {.cell}\n\n```{.r .cell-code}\nnew_names <- tribble(\n ~names_var, ~new_names_var,\n \"var1\", \"Variable 1\",\n \"var2\", \"Variable 2\",\n \"var3\", \"Variable 3\",\n \"var4\", \"Variable 4\"\n)\n\nnew_names_vec <- deframe(select(new_names, new_names_var, names_var))\n```\n:::\n\n\n# Solution using tidy evaluation and base R\n\nOur goal is to unpack the vector of column name pairs that are actually in our data frame. We could achieve this by using unquote-splice `!!!` which will splice the list of names into the dynamic dots `...` of `rename()`.\n\nHowever, the column `var3` is not found. An error appears.\n\n\n::: {.cell}\n\n```{.r .cell-code}\ntest_tib |>\n rename(!!!new_names_vec)\n```\n\n::: {.cell-output .cell-output-error}\n```\nError in `rename()`:\n! Can't rename columns that don't exist.\n✖ Column `var3` doesn't exist.\n```\n:::\n:::\n\n\nSelect only the variables which are in the named vector `new_names_vec`.\n\n\n::: {.cell}\n\n```{.r .cell-code}\ntest_tib |>\n rename(!!!new_names_vec[new_names_vec %in% names(test_tib)])\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n# A tibble: 3 × 3\n `Variable 1` `Variable 2` `Variable 4`\n <chr> <chr> <int>\n1 x a 1\n2 y b 2\n3 z c 3\n```\n:::\n:::\n\n\n# Solution using dplyr\n\nInstead of selecting the common variables, you can use `any_of()` which does this selection automatically.\n\n\n::: {.cell}\n\n```{.r .cell-code}\ntest_tib |>\n rename(any_of(new_names_vec))\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n# A tibble: 3 × 3\n `Variable 1` `Variable 2` `Variable 4`\n <chr> <chr> <int>\n1 x a 1\n2 y b 2\n3 z c 3\n```\n:::\n:::\n\n\n\n# Sources\n\nThese examples are inspired by:\n\n- [Article written by Tim Tiefenbach](https://tim-tiefenbach.de/post/2022-rename-columns/#dplyr-tidyverse)\n\n- [https://dcl-prog.stanford.edu/tidy-eval-detailed.html](https://dcl-prog.stanford.edu/tidy-eval-detailed.html)\n\n- [https://adv-r.hadley.nz/quasiquotation.html#unquoting-many-arguments](https://adv-r.hadley.nz/quasiquotation.html#unquoting-many-arguments)\n\n- [https://rlang.r-lib.org/reference/topic-inject.html#splicing-with--1](https://rlang.r-lib.org/reference/topic-inject.html#splicing-with--1)\n\n- [https://rlang.r-lib.org/reference/dyn-dots.html](https://rlang.r-lib.org/reference/dyn-dots.html)\n", | ||
"supporting": [], | ||
"filters": [ | ||
"rmarkdown/pagebreak.lua" | ||
], | ||
"includes": {}, | ||
"engineDependencies": {}, | ||
"preserve": {}, | ||
"postProcess": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
--- | ||
title: "Rename variables in a data frame using an external lookup table" | ||
author: "Layal C. Lettry" | ||
date: "2023-10-08" | ||
categories: [unquote-splice, tidy evaluation, rename, any_of] | ||
image: "image.jpg" | ||
--- | ||
|
||
# Rename variables in a data frame using an external lookup table | ||
|
||
Suppose that a data frame is present with certain columns that possess the appropriate names, however, the remaining columns require renaming. An existing lookup table is ready to be used for setting new names to these specific columns. | ||
|
||
I found the solution by using tidy evaluation tools, namely the unquote-splice `!!!`, and by reading the [article written by Tim Tiefenbach](https://tim-tiefenbach.de/post/2022-rename-columns/#dplyr-tidyverse). | ||
|
||
```{r} | ||
#| label: load_libraries | ||
#| message: false | ||
#| warning: false | ||
library(tidyverse) | ||
``` | ||
|
||
Here is the data frame with 3 variables, namely `var1`, `var2` and `var4`. | ||
|
||
```{r} | ||
#| label: data | ||
#| message: false | ||
#| warning: false | ||
test_tib <- tribble( | ||
~var1, ~var2, ~var4, | ||
"x", "a", 1L, | ||
"y", "b", 2L, | ||
"z", "c", 3L | ||
) | ||
``` | ||
|
||
Define the lookup table with the new names. Transform this lookup table into a named vector using `deframe()`. Do not forget that the first argument of `deframe()` should be the new names of the variable and the second one should have the actual names. | ||
|
||
```{r} | ||
#| label: lookup | ||
#| message: false | ||
#| warning: false | ||
new_names <- tribble( | ||
~names_var, ~new_names_var, | ||
"var1", "Variable 1", | ||
"var2", "Variable 2", | ||
"var3", "Variable 3", | ||
"var4", "Variable 4" | ||
) | ||
new_names_vec <- deframe(select(new_names, new_names_var, names_var)) | ||
``` | ||
|
||
# Solution using tidy evaluation and base R | ||
|
||
Our goal is to unpack the vector of column name pairs that are actually in our data frame. We could achieve this by using unquote-splice `!!!` which will splice the list of names into the dynamic dots `...` of `rename()`. | ||
|
||
However, the column `var3` is not found. An error appears. | ||
|
||
```{r} | ||
#| label: error | ||
#| error: true | ||
#| message: false | ||
#| warning: false | ||
test_tib |> | ||
rename(!!!new_names_vec) | ||
``` | ||
|
||
Select only the variables which are in the named vector `new_names_vec`. | ||
|
||
```{r} | ||
#| label: base_r_solution | ||
#| message: false | ||
#| warning: false | ||
test_tib |> | ||
rename(!!!new_names_vec[new_names_vec %in% names(test_tib)]) | ||
``` | ||
|
||
# Solution using dplyr | ||
|
||
Instead of selecting the common variables, you can use `any_of()` which does this selection automatically. | ||
|
||
```{r} | ||
#| label: dplyr_solution | ||
#| message: false | ||
#| warning: false | ||
test_tib |> | ||
rename(any_of(new_names_vec)) | ||
``` | ||
|
||
|
||
# Sources | ||
|
||
These examples are inspired by: | ||
|
||
- [Article written by Tim Tiefenbach](https://tim-tiefenbach.de/post/2022-rename-columns/#dplyr-tidyverse) | ||
|
||
- [https://dcl-prog.stanford.edu/tidy-eval-detailed.html](https://dcl-prog.stanford.edu/tidy-eval-detailed.html) | ||
|
||
- [https://adv-r.hadley.nz/quasiquotation.html#unquoting-many-arguments](https://adv-r.hadley.nz/quasiquotation.html#unquoting-many-arguments) | ||
|
||
- [https://rlang.r-lib.org/reference/topic-inject.html#splicing-with--1](https://rlang.r-lib.org/reference/topic-inject.html#splicing-with--1) | ||
|
||
- [https://rlang.r-lib.org/reference/dyn-dots.html](https://rlang.r-lib.org/reference/dyn-dots.html) |
Binary file not shown.
This file was deleted.
Oops, something went wrong.