I want to hand over the name of a variable as a string to the order_by argument of dplyr::slice_max(), but I'm getting weird results. Using the iris data here.
library(dplyr)
lastcol <- first(colnames(iris))
My expected result would be a df/tibble of 5 rows, which works while using the actual column name.
i <- iris %>%
slice_max(n = 5, order_by = Sepal.Length)
But when handing over the column name, I'm either getting unexpected results or errors.
# unexpected result: one row
i <- iris %>%
slice_max(n = 5, order_by = lastcol)
# unexpected result: one row
i <- iris %>%
slice_max(n = 5, order_by = !!lastcol)
# Error: `:=` can only be used within a quasiquoted argument
i <- iris %>%
slice_max(n = 5, order_by := .data[[lastcol]])
What's missing?
I am not sure what
newstrdelcolis. But if you want to use what you have defined inlastcolyou can do it withget(lastcol)like this: