File: external-vector.Rmd

package info (click to toggle)
r-cran-tidyselect 1.2.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 616 kB
  • sloc: sh: 13; makefile: 2
file content (64 lines) | stat: -rw-r--r-- 1,588 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

```{r, child = "setup.Rmd", include = FALSE}
```

## Ambiguity between columns and external variables

With selecting functions like `dplyr::select()` or `tidyr::pivot_longer()`,
you can refer to variables by name:

```{r}
mtcars %>% select(cyl, am, vs)

mtcars %>% select(mpg:disp)
```

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.

```{r}
vars <- c("cyl", "am", "vs")
result <- mtcars %>% select(vars)
```

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.

```{r}
some_df <- mtcars[1:4, ]
some_df$vars <- 1:nrow(some_df)
```

These are very different objects but it isn't a problem if the context
forces you to be specific about where to find `vars`:

```{r}
vars

some_df$vars
```

In a selection context however, the column wins:

```{r}
some_df %>% select(vars)
```


## Fixing the ambiguity

To make your selection code more robust and silence the message, use
`all_of()` to force the external vector:

```{r}
some_df %>% select(all_of(vars))
```

For more information or if you have comments about this, please see
the [Github issue](https://github.com/r-lib/tidyselect/issues/76)
tracking the deprecation process.