EVOLUTION-MANAGER
Edit File: faq-external-vector.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: FAQ - Note: Using an external vector in selections is...</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for faq-external-vector {tidyselect}"><tr><td>faq-external-vector {tidyselect}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>FAQ - Note: Using an external vector in selections is ambiguous</h2> <h3>Description</h3> <h4>Ambiguity between columns and external variables</h4> <p>With selecting functions like <code>dplyr::select()</code> or <code>tidyr::pivot_longer()</code>, you can refer to variables by name:<div class="r"></p> <pre>mtcars %>% select(cyl, am, vs) #> # A tibble: 32 x 3 #> cyl am vs #> <dbl> <dbl> <dbl> #> 1 6 1 0 #> 2 6 1 0 #> 3 4 1 1 #> 4 6 0 1 #> # ... with 28 more rows mtcars %>% select(mpg:disp) #> # A tibble: 32 x 3 #> mpg cyl disp #> <dbl> <dbl> <dbl> #> 1 21 6 160 #> 2 21 6 160 #> 3 22.8 4 108 #> 4 21.4 6 258 #> # ... with 28 more rows </pre></div> <p>For historical reasons, it is also possible to refer an external vector of variable names. You get the correct result, but with a note informing you that selecting with an external variable is ambiguous because it is not clear whether you want a data frame column or an external object.<div class="r"></p> <pre>vars <- c("cyl", "am", "vs") result <- mtcars %>% select(vars) #> Note: Using an external vector in selections is ambiguous. #> i Use `all_of(vars)` instead of `vars` to silence this message. #> i See <https://tidyselect.r-lib.org/reference/faq-external-vector.html>. #> This message is displayed once per session. </pre></div> <p>This note will become a warning in the future, and then an error. We have decided to deprecate this particular approach to using external vectors because they introduce ambiguity. Imagine that the data frame contains a column with the same name as your external variable.<div class="r"></p> <pre>some_df <- mtcars[1:4, ] some_df$vars <- 1:nrow(some_df) </pre></div> <p>These are very different objects but it isn’t a problem if the context forces you to be specific about where to find <code>vars</code>:<div class="r"></p> <pre>vars #> [1] "cyl" "am" "vs" some_df$vars #> [1] 1 2 3 4 </pre></div> <p>In a selection context however, the column wins:<div class="r"></p> <pre>some_df %>% select(vars) #> # A tibble: 4 x 1 #> vars #> <int> #> 1 1 #> 2 2 #> 3 3 #> 4 4 </pre></div> <h4>Fixing the ambiguity</h4> <p>To make your selection code more robust and silence the message, use <code>all_of()</code> to force the external vector:<div class="r"></p> <pre>some_df %>% select(all_of(vars)) #> # A tibble: 4 x 3 #> cyl am vs #> <dbl> <dbl> <dbl> #> 1 6 1 0 #> 2 6 1 0 #> 3 4 1 1 #> 4 6 0 1 </pre></div> <p>For more information or if you have comments about this, please see the <a href="https://github.com/r-lib/tidyselect/issues/76">Github issue</a> tracking the deprecation process. </p> <hr /><div style="text-align: center;">[Package <em>tidyselect</em> version 1.1.0 <a href="00Index.html">Index</a>]</div> </body></html>