| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/faq.R |
| 3 |
\name{faq-external-vector} |
| 4 |
\alias{faq-external-vector} |
| 5 |
\title{FAQ - Note: Using an external vector in selections is ambiguous} |
| 6 |
\description{ |
| 7 |
\subsection{Ambiguity between columns and external variables}{ |
| 8 |
|
| 9 |
With selecting functions like \code{dplyr::select()} or |
| 10 |
\code{tidyr::pivot_longer()}, you can refer to variables by name: |
| 11 |
|
| 12 |
\if{html}{\out{<div class="sourceCode r">}}\preformatted{mtcars \%>\% select(cyl, am, vs) |
| 13 |
#> # A tibble: 32 x 3 |
| 14 |
#> cyl am vs |
| 15 |
#> <dbl> <dbl> <dbl> |
| 16 |
#> 1 6 1 0 |
| 17 |
#> 2 6 1 0 |
| 18 |
#> 3 4 1 1 |
| 19 |
#> 4 6 0 1 |
| 20 |
#> # i 28 more rows |
| 21 |
|
| 22 |
mtcars \%>\% select(mpg:disp) |
| 23 |
#> # A tibble: 32 x 3 |
| 24 |
#> mpg cyl disp |
| 25 |
#> <dbl> <dbl> <dbl> |
| 26 |
#> 1 21 6 160 |
| 27 |
#> 2 21 6 160 |
| 28 |
#> 3 22.8 4 108 |
| 29 |
#> 4 21.4 6 258 |
| 30 |
#> # i 28 more rows |
| 31 |
}\if{html}{\out{</div>}} |
| 32 |
|
| 33 |
For historical reasons, it is also possible to refer an external vector |
| 34 |
of variable names. You get the correct result, but with a warning |
| 35 |
informing you that selecting with an external variable is ambiguous |
| 36 |
because it is not clear whether you want a data frame column or an |
| 37 |
external object. |
| 38 |
|
| 39 |
\if{html}{\out{<div class="sourceCode r">}}\preformatted{vars <- c("cyl", "am", "vs") |
| 40 |
result <- mtcars \%>\% select(vars) |
| 41 |
#> Warning: Using an external vector in selections was deprecated in tidyselect |
| 42 |
#> 1.1.0. |
| 43 |
#> i Please use `all_of()` or `any_of()` instead. |
| 44 |
#> # Was: |
| 45 |
#> data \%>\% select(vars) |
| 46 |
#> |
| 47 |
#> # Now: |
| 48 |
#> data \%>\% select(all_of(vars)) |
| 49 |
#> |
| 50 |
#> See |
| 51 |
#> <https://tidyselect.r-lib.org/reference/faq-external-vector.html>. |
| 52 |
#> This warning is displayed once every 8 hours. |
| 53 |
#> Call `lifecycle::last_lifecycle_warnings()` to see where this |
| 54 |
#> warning was generated. |
| 55 |
}\if{html}{\out{</div>}} |
| 56 |
|
| 57 |
We have decided to deprecate this particular approach to using external |
| 58 |
vectors because they introduce ambiguity. Imagine that the data frame |
| 59 |
contains a column with the same name as your external variable. |
| 60 |
|
| 61 |
\if{html}{\out{<div class="sourceCode r">}}\preformatted{some_df <- mtcars[1:4, ] |
| 62 |
some_df$vars <- 1:nrow(some_df) |
| 63 |
}\if{html}{\out{</div>}} |
| 64 |
|
| 65 |
These are very different objects but it isn’t a problem if the context |
| 66 |
forces you to be specific about where to find \code{vars}: |
| 67 |
|
| 68 |
\if{html}{\out{<div class="sourceCode r">}}\preformatted{vars |
| 69 |
#> [1] "cyl" "am" "vs" |
| 70 |
|
| 71 |
some_df$vars |
| 72 |
#> [1] 1 2 3 4 |
| 73 |
}\if{html}{\out{</div>}} |
| 74 |
|
| 75 |
In a selection context however, the column wins: |
| 76 |
|
| 77 |
\if{html}{\out{<div class="sourceCode r">}}\preformatted{some_df \%>\% select(vars) |
| 78 |
#> # A tibble: 4 x 1 |
| 79 |
#> vars |
| 80 |
#> <int> |
| 81 |
#> 1 1 |
| 82 |
#> 2 2 |
| 83 |
#> 3 3 |
| 84 |
#> 4 4 |
| 85 |
}\if{html}{\out{</div>}} |
| 86 |
} |
| 87 |
|
| 88 |
\subsection{Fixing the ambiguity}{ |
| 89 |
|
| 90 |
To make your selection code more robust and silence the message, use |
| 91 |
\code{all_of()} to force the external vector: |
| 92 |
|
| 93 |
\if{html}{\out{<div class="sourceCode r">}}\preformatted{some_df \%>\% select(all_of(vars)) |
| 94 |
#> # A tibble: 4 x 3 |
| 95 |
#> cyl am vs |
| 96 |
#> <dbl> <dbl> <dbl> |
| 97 |
#> 1 6 1 0 |
| 98 |
#> 2 6 1 0 |
| 99 |
#> 3 4 1 1 |
| 100 |
#> 4 6 0 1 |
| 101 |
}\if{html}{\out{</div>}} |
| 102 |
|
| 103 |
For more information or if you have comments about this, please see the |
| 104 |
\href{https://github.com/r-lib/tidyselect/issues/76}{Github issue} tracking |
| 105 |
the deprecation process. |
| 106 |
} |
| 107 |
} |