Questions tagged [tidyselect]

Questions about `r` package `tidyselect` used in many functions from the `tidyverse` packages

88 questions
4
votes
3 answers

Failure to rename duplicate column names with dplyr rename() and rename_with()

Given a tibble or data.frame that has duplicate column names, I want to use dplyr::rename or dplyr::rename_with to either: (a) differentiate the duplicate names with a serial numeric suffix ('a_1', 'a_2', etc) or (b) rename each column…
andrew_reece
  • 20,390
  • 3
  • 33
  • 58
4
votes
3 answers

Since .key is deprecated how it is possible to rename the data column in nest()?

Before .key was deprecated I did this: library(tidyverse) mtcars %>% group_by(cyl) %>% nest(.key = "my_name") The help of nest() points out that now this is performed using tidy select, but I don't know how.
danilinares
  • 1,172
  • 1
  • 9
  • 28
4
votes
5 answers

R Replace NA for all Columns Except *

library(tidyverse) df <- tibble(Date = c(rep(as.Date("2020-01-01"), 3), NA), col1 = 1:4, thisCol = c(NA, 8, NA, 3), thatCol = 25:28, col999 = rep(99, 4)) #> # A tibble: 4 x 5 #> Date col1 …
Display name
  • 4,153
  • 5
  • 27
  • 75
4
votes
1 answer

How can I capture the changes made to a data.frame in dplyr::select?

I would like to create a subclass of data.frame that carries around some information about the state of particular columns. I thought the best way to do this would be with an attribute, special_col. A simple constructor seems to work fine: # Light…
Calum You
  • 14,687
  • 4
  • 23
  • 42
4
votes
1 answer

Excluding multiple columns based on unquote-splicing (!!!)

Trying to exclude multiple columns in a call to tidyr::gather() which are served as inputs to my function via a character vector argument (output of shiny::selectInput) instead of via ... in a programmatic way How would I do that with tidy eval…
Rappster
  • 12,762
  • 7
  • 71
  • 120
4
votes
3 answers

Arrange data frame by all columns using dplyr

I am generating data frames of 1s and 0s as follows: library(tidyverse) library(glue) num_var <- 3 rep(list(c(0L, 1L)), num_var) %>% set_names(glue("var_{seq_len(num_var)}")) %>% expand.grid() %>% mutate(total = rowSums(.)) %>% …
Jake Thompson
  • 2,591
  • 1
  • 16
  • 32
3
votes
1 answer

How to select row/column variables starting with a particular set of characters (e.g., Q4) in R package expss?

In survey research we often have blocks of variables starting with the same set of characters such as "Q4a", "Q4b", etc. Is it possible to avoid manually entering column names starting with "Q4" as arguments of the tab_cells function of expss…
statadvice
  • 45
  • 5
3
votes
2 answers

Apply dplyr::starts_with() with lambda function

I have below implementation library(dplyr) library(tidyr) dat = data.frame('A' = 1:3, 'C_1' = 1:3, 'C_2' = 1:3, 'M' = 1:3) Below works dat %>% rowwise %>% mutate(Anew = list({function(x) c(x[1]^2, x[2] + 5, x[3] + 1)}(c(M, C_1, C_2)))) %>% ungroup…
Brian Smith
  • 1,200
  • 4
  • 16
3
votes
3 answers

Replace NAs based on conditions in R

I have a dataset and I want to replace NAs with empty string in those columns where the number of missing values is greater or equal to n. For instance, n = 500. set.seed(2022) synthetic <- tibble( col1 = runif(1000), col2 = runif(1000), col3…
rg4s
  • 811
  • 5
  • 22
3
votes
2 answers

How to use dynamic tidy-select expressions in dplyr::select()?

I need to select variables dynamically on multiple expressions. Consider the following example: library(tidyverse) set.seed(42) df <- tibble( grey_dog = runif(n = 69), white_bear = runif(n = 69), blue_oyster = runif(n = 69), white_lobster =…
Comfort Eagle
  • 2,112
  • 2
  • 22
  • 44
3
votes
1 answer

Using quoted variables in a custom dplyr wrapper function

My problem is the following. I have a function foo which works inside dplyr::mutate. This function accepts tidyselect syntax. I want to build a wrapper function bar which should also support tidyselect syntax. I am looking for a clean way to pass…
TimTeaFan
  • 17,549
  • 4
  • 18
  • 39
3
votes
1 answer

How to reconcile .data pronouns with rlang::enquo

I am writing a package using a lot of dplyr functions - to pass on all tests in devtools::check(), I have to use .data frequently. Some of the functions are nested into other functions. In the example below, I need to use variable both in a…
3
votes
1 answer

Why the tidyselect helper function "where" can be detected inside the dplyr helper function "across"?

The "tidyselect" package offers a select helper function where. where is used to select dataframe columns with a custom function. It is an internal function from "tidyselect". That means where will not be loaded to your namespace and you can only…
3
votes
1 answer

Write your own tidyselect functions

I wrote an R package that utilizes the {tidyselect} selectors (e.g. contains(), starts_with(), etc.). I would like to add a few more select helper functions to the package to select variables based on some attribute. For example, select all…
Daniel D. Sjoberg
  • 8,820
  • 2
  • 12
  • 28
3
votes
2 answers

Error in inDL(x, as.logical(local), as.logical(now), ...) : unable to load shared object

I'm having this error when trying to attach package tidyselect and when trying to call tidyselect::any_function. The error happens in Rstudio or command line alike, in RStudio it is triggered as soon as I type: tidyselect:: , though ?tidyselect::…
moodymudskipper
  • 46,417
  • 11
  • 121
  • 167