Home > database >  dplyr `slice_max` interpolation not working
dplyr `slice_max` interpolation not working

Time:10-01

Given a data.frame:

library(tidyverse)
set.seed(0)
df <- tibble(A = 1:10, B = rnorm(10), C = rbinom(10,2,0.6))
var <- "B"

I'd like to get filter the data frame by the highest values of the variable in var. Logically, I'd do either:

df %>%
    slice_max({{ var }}, n = 5)
#> # A tibble: 1 × 3
#>       A     B     C
#>   <int> <dbl> <int>
#> 1     1  1.26     1
df %>%
    slice_max(!! var, n = 5)
#> # A tibble: 1 × 3
#>       A     B     C
#>   <int> <dbl> <int>
#> 1     1  1.26     1

But neither interpolation is working... what am I missing here?

Expected output would be the same as:

df %>%
    slice_max(B, n = 5)
#> # A tibble: 5 × 3
#>       A     B     C
#>   <int> <dbl> <int>
#> 1    10 2.40      0
#> 2     3 1.33      2
#> 3     4 1.27      1
#> 4     1 1.26      1
#> 5     5 0.415     2

CodePudding user response:

I think you need to use the newer .data version as outlined here:

df %>%
  slice_max(.data[[var]] , n = 5)

#> # A tibble: 5 × 3
#>       A     B     C
#>   <int> <dbl> <int>
#> 1    10 2.40      0
#> 2     3 1.33      2
#> 3     4 1.27      1
#> 4     1 1.26      1
#> 5     5 0.415     2

I am puzzled by why your approach is get the first row only though!

CodePudding user response:

We may convert to sym and evaluate (!!)

library(dplyr)
df %>%
    slice_max(!! rlang::sym(var), n = 5)

-output

# A tibble: 5 × 3
      A     B     C
  <int> <dbl> <int>
1    10 2.40      0
2     3 1.33      2
3     4 1.27      1
4     1 1.26      1
5     5 0.415     2
  • Related