Questions about `r` package `tidyselect` used in many functions from the `tidyverse` packages
Questions tagged [tidyselect]
88 questions
19
votes
3 answers
dplyr::select() with some variables that may not exist in the data frame?
I have a helper function (say foo()) that will be run on various data frames that may or may not contain specified variables. Suppose I have
library(dplyr)
d1 <- data_frame(taxon=1,model=2,z=3)
d2 <- data_frame(taxon=2,pss=4,z=3)
The variables I…

Ben Bolker
- 211,554
- 25
- 370
- 453
8
votes
3 answers
Sophisticated formula inside arrange
I would like to obtain a generic formula to arrange dataframes with a varying number of columns.
For example, in this case the dataframe contains "categ_1, categ_2, points_1, points_2":
library(tidyverse)
set.seed(1)
nrows <- 20
df <-…

crestor
- 1,388
- 8
- 21
8
votes
1 answer
tidyselect changes how to refer an external vector of variable names in selecting functions in R
I have started to recieve a warning when using selecting functions within tidyverse packages.
Example:
library(dplyr)
set.seed(123)
df = data.frame(
"id" = c(rep("G1", 3), rep("G2", 4), rep("G3", 3)),
"total" = sample.int(n = 10),
"C1" =…

alvaropr
- 699
- 9
- 20
7
votes
1 answer
replace_na with tidyselect?
Suppose I have a data frame with a bunch of columns where I want to do the same NA replacement:
dd <- data.frame(x = c(NA, LETTERS[1:4]), a = rep(NA_real_, 5), b = c(1:4, NA))
For example, in the data frame above I'd like to do something like…

Ben Bolker
- 211,554
- 25
- 370
- 453
7
votes
1 answer
Mutate multiple variable to create multiple new variables
Let's say I have a tibble where I need to take multiple variables and mutate them into new multiple new variables.
As an example, here is a simple tibble:
tb <- tribble(
~x, ~y1, ~y2, ~y3, ~z,
1,2,4,6,2,
2,1,2,3,3,
3,6,4,2,1
)
I want to…

Jacob Nelson
- 443
- 1
- 6
- 16
6
votes
2 answers
Can I group_by columns with starts_with?
I'm dealing with a big dataframe that has a number of columns I want to group by. I'd like to do something like this:
output <- df %>%
group_by(starts_with("GEN", ignore.case=TRUE),x,y) %>%
summarize(total=n()) %>%
arrange(desc(total))
is…

Andrew P
- 63
- 4
6
votes
1 answer
Specify the dots argument when calling a tidyselect-using function without needing to specify the preceding arguments
Here's a simplified version of a function I have in a package that uses the ... argument and tidyselect to select variables:
# this toy function just selects the ... variables
foo <- function(dat = mtcars, ...){
expr <- rlang::expr(c(...))
…

Sam Firke
- 21,571
- 9
- 87
- 105
6
votes
1 answer
Can you list an exception to tidyselect `everything()`
library(tidyverse)
iris %>% as_tibble() %>% select(everything())
#> # A tibble: 150 x 5
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#>
#> 1 5.1 3.5 …

Display name
- 4,153
- 5
- 27
- 75
6
votes
2 answers
dplyr::filter "No tidyselect variables were registered"
I am trying to filter specific rows of my tibble using the dplyr::filter() function.
Here is part of my tibble head(raw.tb):
A tibble: 738 x 4
geno ind X Y
1 san1w16 A1 467 383
2 san1w16 A1 …

Al3xEP
- 328
- 2
- 9
5
votes
2 answers
R: Tidyverse selection semantics tidyselect::eval_select appending numbers to duplicates
I am trying for some time to understand tidyverse design and how to program with it. I was trying to write a function that uses tidyselect semantics, and I found that tidyselect::eval_select appends numbers to lhs expressions. This was not…

Claudiu Papasteri
- 2,469
- 1
- 17
- 30
4
votes
1 answer
Specify a column type across multiple columns with tidy-selection in readr package
I attempt to use read_csv from {readr} to read a CSV file into R. To demonstrate my real issue, I reset the argument guess_max to 5 at first (default is 1000)
library(readr)
formals(read_csv)$guess_max <- 5
and take a smaller literal data for…

user18894435
- 373
- 1
- 10
4
votes
5 answers
dplyr rowwise summarise by column, grouped by name
Let's consider this simple dataset
set.seed(12345)
df <- data.frame(a1 = rnorm(5), a2 = rnorm(5), a3 = rnorm(5),
b1 = rnorm(5), b2 = rnorm(5), b3 = rnorm(5),
c1 = rnorm(5), c2 = rnorm(5), c3 = rnorm(5))
Which…

nico
- 50,859
- 17
- 87
- 112
4
votes
1 answer
Using eval_select inside of a nested function
I'm trying to use tidyselect::eval_select() inside a nested function, but am struggling to work out how to pass through user arguments. Inside 1 level of nesting, this works fine:
library(tidyselect)
library(rlang)
my_function <- function(.x, ...)…

thisisnic
- 820
- 5
- 10
4
votes
2 answers
Issue with selecting negative values in dplyr with embrace {{ arg }}
I have an issue with selecting negative columns based on a variable.
I found a similar issue reported here:
https://github.com/tidyverse/dplyr/issues/4813 but the provided solution does not work (see repex below).
If anyone knows a workaround, that…

Baraliuh
- 2,009
- 5
- 11
4
votes
2 answers
Programming with dplyr: Renaming a column with variable using glue syntax
I've read through Programming with dplyr and understand that rename() and select() use tidy selection. I'm trying to combine this with the glue syntax to create a custom function using the new double curly syntax (rlang v0.4.0), however I'm getting…

Alwin
- 321
- 2
- 14