Using unquoted strings in with `dplyr` verbs: `select` and `arrange` working differently

余生颓废 提交于 2021-01-04 04:59:10

问题


I am trying to unquote a string for use in dplyr::arrange. It does not appear to work. However, it appears to work correctly in dplyr::select. Am I missing something or doing something wrong here?


library(tidyverse)

df <- tibble(x = c(1, 2, 3),
             y = c(8, 6, 3))

v <- 'y'

# `select` works with `!!v` 

df %>% select(y)
#> # A tibble: 3 x 1
#>       y
#>   <dbl>
#> 1     8
#> 2     6
#> 3     3

df %>% select(!!v)
#> # A tibble: 3 x 1
#>       y
#>   <dbl>
#> 1     8
#> 2     6
#> 3     3

# `arrange` not work with `!!v`

df %>% arrange(y)
#> # A tibble: 3 x 2
#>       x     y
#>   <dbl> <dbl>
#> 1     3     3
#> 2     2     6
#> 3     1     8

df %>% arrange(!!v)
#> # A tibble: 3 x 2
#>       x     y
#>   <dbl> <dbl>
#> 1     1     8
#> 2     2     6
#> 3     3     3

回答1:


You need to convert your string to variable first (using sym()) then unquote it inside arrange().

df %>% arrange(!!sym(v))

#> # A tibble: 3 x 2
#>       x     y
#>   <dbl> <dbl>
#> 1     3     3
#> 2     2     6
#> 3     1     8

select() can directly take string input but it's not recommended

df %>% select(v)

#> Note: Using an external vector in selections is ambiguous.
#> i Use `all_of(v)` instead of `v` to silence this message.
#> i See <https://tidyselect.r-lib.org/reference/faq-external-vector.html>.
#> This message is displayed once per session.
#> # A tibble: 3 x 1
#>       y
#>   <dbl>
#> 1     8
#> 2     6
#> 3     3

Created on 2020-11-21 by the reprex package (v0.3.0)




回答2:


In selecting verbs, use all_of() or any_of(). the former causes an error if not all variables are present in the data frame, the latter is lenient (and in general more useful for deselecting)

df %>% select(all_of(v))
#> # A tibble: 3 x 1
#>       y
#>   <dbl>
#> 1     8
#> 2     6
#> 3     3

In action verbs, like arrange() or mutate(), select single variables by subsetting the .data pronoun:

df %>% arrange(.data[[v]])
#> # A tibble: 3 x 2
#>       x     y
#>   <dbl> <dbl>
#> 1     3     3
#> 2     2     6
#> 3     1     8

You can also use selections in action verbs by using across():

df %>% arrange(across(starts_with("y")))
#> # A tibble: 3 x 2
#>       x     y
#>   <dbl> <dbl>
#> 1     3     3
#> 2     2     6
#> 3     1     8

Which means you can use all_of() as well to select from a character vector:

df %>% arrange(across(all_of(v)))
#> # A tibble: 3 x 2
#>       x     y
#>   <dbl> <dbl>
#> 1     3     3
#> 2     2     6
#> 3     1     8


来源:https://stackoverflow.com/questions/64946231/using-unquoted-strings-in-with-dplyr-verbs-select-and-arrange-working-dif

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!