Questions tagged [tidyselect]

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

88 questions
2
votes
3 answers

What is the recommended alternative for the deprecated .data[[ ]] in mutate()?

As of tidyselect 1.2.0 the use of .data[[ ]] is now deprecated. Now I wonder how you can alias a column while using a vector with the column name of the target column. What I used to do: mycolname <- 'cyl' mtcars %>% mutate(newcol =…
saQuist
  • 416
  • 7
  • 19
2
votes
1 answer

Is it possible to use tidyselect helpers with the cols_only() function?

I have a .csv file like this (except that the real .csv file has many more columns): library(tidyverse) tibble(id1 = c("a", "b"), id2 = c("c", "d"), data1 = c(1, 2), data2 = c(3, 4), data1s = c(5, 6), data2s =…
nicholas
  • 903
  • 2
  • 12
2
votes
1 answer

Custom function: Return a tidyselect argument as character-string, without executing the argument

I would like to create a segment in my custom function where the input of the argument "evars" is returned as a character string, without executing/evaluating the argument. The argument is a tidyselect function used to extract specified columns from…
daems013
  • 45
  • 5
2
votes
1 answer

Check if a tidyselect is NULL and if so, set a default tidyselect method

I am struggling to set a default selection method if NULL is given. For example, let's say I want to implement a function that squares all values for some tidyselect method, symbol, or string, and if none is given, then by default it squares all…
Baraliuh
  • 2,009
  • 5
  • 11
2
votes
0 answers

Unable to find an inherited method for function ‘coordinates’ for signature ‘"tbl_df"’

When I was trying to create a spatial point data frame in R a sudden error appeared that made it unable for me to create a new SpatialPointdataFrame. The odd thing was that earlier I used the exact same code as before and it succeeded…
2
votes
2 answers

Pass column names to dplyr::coalesce() when writing a custom function

I'm trying to write a function that will wrap dplyr::coalesce(), and will take in a data object and column names to coalesce. So far, my attempts have failed. Example data library(dplyr) df <- data.frame(col_a = c("bob", NA, "bob", NA, "bob"), …
Emman
  • 3,695
  • 2
  • 20
  • 44
2
votes
1 answer

R: adapt mutate call from handling three binary variables to n binary variables

I've got a dataframe with 3 binary variables that relate to time period 1 and three corresponding variables that relate to time 2. df <- data.frame("user" = c("a","b","c","d","e"), "item_1_time_1" = c(1,0,0,0,NA), "item_2_time_1" = c(1,1,1,0,NA),…
JRR
  • 578
  • 5
  • 21
2
votes
1 answer

pivot_longer with tidyselect where, where but doesnt predicates?

I am trying to use the tidyselect function where with pivot_longer and am getting the error that the tidyselect package doesn't support predicates. That was seem somewhat unreasonable, so most likely I have a syntax error. (I realize that SO is…
Harlan Nelson
  • 1,394
  • 1
  • 10
  • 22
2
votes
2 answers

How to initialize a variable with a tidyselect helper?

I'm using tidyselection in some function, and I have to concatenate the first argument with the ellipsis as it could be a specific class that would need a specific treatment. The normal behavior is this: foo = function(x, ...){ xloc =…
Dan Chaltiel
  • 7,811
  • 5
  • 47
  • 92
2
votes
1 answer

Use tidyselect select helpers in functions that do not implement them

How to use dplyr/tidyselect "select helpers", such as : to select a range of consecutive variables, in functions that do not implement them? If possible in a simple/elegant manner (of course this is subjective). Here is an example with…
Aurèle
  • 12,545
  • 1
  • 31
  • 49
2
votes
1 answer

Unable to use tidyselect `everything()` in combination with `group_by()` and `fill()`

library(tidyverse) df <- tibble(x1 = c("A", "A", "A", "B", "B", "B"), x2 = c(NA, 8, NA, NA, NA, 5), x3 = c(3, 6, 5, 9, 1, 9)) #> # A tibble: 6 x 3 #> x1 x2 x3 #> #> 1 A NA 3 #> 2 A …
Display name
  • 4,153
  • 5
  • 27
  • 75
2
votes
2 answers

Use starts_with inside map instead of explicitly naming

I want to apply a function inside a tibble but I don't want to explicitly Name the columns. E.G. library(tidyverse) library(tidyselect) test = tibble(var1 = c("la", "le", "lu"), var2 = c("ma", "me", "mu"), var3 = c("fi", "fa", "fu"),…
MichaelA
  • 1,866
  • 2
  • 23
  • 38
2
votes
1 answer

regex (in gathering multiple sets of columns with tidyr)

inspired by hadley's nifty gather approach in this answer I tried to use tidyr's gather() and spread() in combination with a regular expression, regex, but I seem to get it wrong on the regex. I did study several regex questions; this one, this one,…
Eric Fail
  • 8,191
  • 8
  • 72
  • 128
2
votes
1 answer

dplyr everything() argument string select_

I need to use the underscored string version of select in dplyr along with the everything() argument. It is not working. library(dplyr) #this works just fine select(iris, Species, everything()) %>% head() Species Sepal.Length Sepal.Width…
Nick Criswell
  • 1,733
  • 2
  • 16
  • 32
1
vote
2 answers

mutate_at with all_of() in r?

Trying to future proof some code here. I was unable to figure out this following warning message: snpdata <- snpdata %>% mutate_at(vars(snpcol_start:snpcol_end), list(~ ifelse(. == 0, "AA", ifelse(. == 1, "AB", .)))) Warning…