tidyverse / dplyr

dplyr: A grammar of data manipulation
https://dplyr.tidyverse.org/
Other
4.75k stars 2.12k forks source link

Feature request: pull_at and "special functions" from select for pull #4060

Closed vnijs closed 5 years ago

vnijs commented 5 years ago

pull is a very nice complement to select. I wonder if it might be useful to add a pull_at type function to dplyr and make some of the special functions you can use in select (e.g., "starts_with") available in pull as well. Some relevant examples in a regex below

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

## extacting a column from mtcars
pull(mtcars, vs)
#>  [1] 0 0 1 1 0 1 0 1 1 1 1 0 0 0 0 0 0 1 1 1 1 0 0 0 0 1 0 1 0 0 0 1
pull(mtcars, "vs")
#>  [1] 0 0 1 1 0 1 0 1 1 1 1 0 0 0 0 0 0 1 1 1 1 0 0 0 0 1 0 1 0 0 0 1

## this works like an _at function in dplyr
my_var <- "vs"
pull(mtcars, my_var)
#>  [1] 0 0 1 1 0 1 0 1 1 1 1 0 0 0 0 0 0 1 1 1 1 0 0 0 0 1 0 1 0 0 0 1

## but if gear is also a variable in the data we may not get the expected result
gear <- "vs"
pull(mtcars, gear)
#>  [1] 4 4 4 3 3 3 3 4 4 4 4 3 3 3 3 3 3 4 4 4 3 3 3 3 3 4 5 5 5 5 5 4

## we could of course also use tidyeval
pull(mtcars, !! gear)
#>  [1] 0 0 1 1 0 1 0 1 1 1 1 0 0 0 0 0 0 1 1 1 1 0 0 0 0 1 0 1 0 0 0 1

## perhaps it would also be nice if the following worked like in select
pull(mtcars, matches("vs"))
#> Error: No tidyselect variables were registered
# select(mtcars, matches("vs"))

## although this does also work
pull(mtcars, which(colnames(mtcars) == "vs"))
#>  [1] 0 0 1 1 0 1 0 1 1 1 1 0 0 0 0 0 0 1 1 1 1 0 0 0 0 1 0 1 0 0 0 1

## but not if gear is a variable in the data
pull(mtcars, which(colnames(mtcars) == gear))
#> Error: `var` must evaluate to a single number or a column name, not an integer vector
pull(mtcars, which(colnames(mtcars) == my_var))
#>  [1] 0 0 1 1 0 1 0 1 1 1 1 0 0 0 0 0 0 1 1 1 1 0 0 0 0 1 0 1 0 0 0 1

Created on 2018-12-30 by the reprex package (v0.2.1)

hadley commented 5 years ago

If you're pulling out a single variable, I think it's better to be explicit about what it is so that you get a clear error message if the data changes for some reason.

vnijs commented 5 years ago

@Hadley Does that apply to the question about pull_at as well?

## but if gear is also a variable in the data we may not get the expected result
gear <- "vs"
pull(mtcars, gear)
#>  [1] 4 4 4 3 3 3 3 4 4 4 4 3 3 3 3 3 3 4 4 4 3 3 3 3 3 4 5 5 5 5 5 4
hadley commented 5 years ago

Just use unquoting: pull(mtcars, !!gear)

lock[bot] commented 5 years ago

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/