Skip to content

Commit

Permalink
fix: print data frames and vectors
Browse files Browse the repository at this point in the history
  • Loading branch information
Layalchristine24 committed Oct 8, 2023
1 parent c143654 commit ce8a972
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"hash": "2c1b7cd21ed9922aade253ecf8ae1982",
"hash": "e011a619a786acecc6faf99647157127",
"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",
"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)\ntest_tib\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n# A tibble: 3 × 3\n var1 var2 var4\n <chr> <chr> <int>\n1 x a 1\n2 y b 2\n3 z c 3\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)\nnew_names\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n# A tibble: 4 × 2\n names_var new_names_var\n <chr> <chr> \n1 var1 Variable 1 \n2 var2 Variable 2 \n3 var3 Variable 3 \n4 var4 Variable 4 \n```\n:::\n\n```{.r .cell-code}\nnew_names_vec <- deframe(select(new_names, new_names_var, names_var))\nnew_names_vec\n```\n\n::: {.cell-output .cell-output-stdout}\n```\nVariable 1 Variable 2 Variable 3 Variable 4 \n \"var1\" \"var2\" \"var3\" \"var4\" \n```\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# 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>\n\n- <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>\n",
"supporting": [],
"filters": [
"rmarkdown/pagebreak.lua"
Expand Down
18 changes: 10 additions & 8 deletions posts/2023-10-08_rename-columns-lookup/index.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ 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.
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).
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
Expand All @@ -32,6 +32,7 @@ test_tib <- tribble(
"y", "b", 2L,
"z", "c", 3L
)
test_tib
```

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.
Expand All @@ -48,8 +49,10 @@ new_names <- tribble(
"var3", "Variable 3",
"var4", "Variable 4"
)
new_names
new_names_vec <- deframe(select(new_names, new_names_var, names_var))
new_names_vec
```

# Solution using tidy evaluation and base R
Expand Down Expand Up @@ -90,17 +93,16 @@ 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)
- [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://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://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/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)
- <https://rlang.r-lib.org/reference/dyn-dots.html>

0 comments on commit ce8a972

Please sign in to comment.